2014-03-12 50 views
0

我已经开发了一个使用Asp.net的网站。在我的程序的一部分中,用户需要下载一些变量文件。 (Jpeg,Pdf,doc,docx ...)当他们按下下载按钮并点击“打开”按钮而不是“保存”按钮时,该文件将用Adobe Acrobat打开。问题是,如果它是一个word文件,它将再次用Adobe Acrobat打开! 我怎样才能避免这一点,并用它自己的程序打开每个文件?如何用链接按钮自己的程序打开文件

protected void LinkButton1_Command(object sender, CommandEventArgs e) 
    { 
     int idx = Convert.ToInt32(e.CommandArgument); 
     string Manuscript = GridView2.DataKeys[idx].Value.ToString(); 

     string fileName = System.IO.Path.GetFileName(Manuscript); 

     Response.Clear(); 
     Response.ContentType = "application/pdf"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
     Response.TransmitFile(Server.MapPath(Manuscript)); 
     Response.End(); 
    } 

回答

1

像这样::

string ext = System.IO.Path.GetExtension(this.File1.PostedFile.FileName); 
string contenttype = String.Empty; 

OR

string ext = Path.GetExtension(filename); 

switch(ext) 
{ 
    case ".doc": 
     contenttype = "application/vnd.ms-word"; 
     break; 
    case ".docx": 
     contenttype = "application/vnd.ms-word"; 
     break; 
    case ".xls": 
     contenttype = "application/vnd.ms-excel"; 
     break; 
    case ".xlsx": 
     contenttype = "application/vnd.ms-excel"; 
     break; 
    case ".jpg": 
     contenttype = "image/jpg"; 
     break; 
    case ".png": 
     contenttype = "image/png"; 
     break; 
    case ".gif": 
     contenttype = "image/gif"; 
     break; 
    case ".pdf": 
     contenttype = "application/pdf"; 
     break; 
} 
+0

谢谢,contenttype不支持我的鳕鱼!它是否需要任何名称空间? – Reza

+1

@SaeedTalaee它是一个字符串类型,你必须decalre。 –

+0

查看此连结: http://www.aspsnippets.com/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx –

1

使用

FileInfo fInfo = new FileInfo(fileName); 
string fileExtn = fInfo.Extension; 
+0

谢谢你,什么是名字空间 我在我的身后鳕鱼用这个鳕鱼FileInfo? – Reza

+1

不客气。它的'System.IO'。检查http://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx。如果我的回答可以帮助你,请注册并接受。 – MusicLovingIndianGirl

+1

你不必担心命名空间,如果你不知道命名空间粘贴代码,并单击FileInfo和Visual Studio将向你建议appropraite命名空间,点击它,它会自动添加 –

相关问题