2012-03-19 53 views
0

我正在开发一个ASP.NET C#应用程序,有一个表格,我在其中定义了一个二进制列来存储文件(列名称attFile)也保存了文件名,文件大小和Mimetype,大部分文件都是JPG,PNG,PDF,我没有问题可以将这些文件以预览图像的方式显示给用户,但是使用像的Excel,Word PowerPoint中,我不知道如何表达他们这样用户可以下载它,我这样做的方式是如何展示用户可以下载的Word,Excel和PowerPoint文件ASP.NET C#

View 
@foreach (var item in Model) 
{ 
    if (item.MimeType == "application/pdf") 
    { 
     <div> 
      <object data="@Url.Action("MyImage", new { id = item.id })" 
           type="application/pdf" 
           width="250" height="150"> 
      </object> 
     </div> 
    } 
    else if (item.fotoMimeType == "image/jpeg") 
    { 
     <div> 
      <img src="@Url.Action("MyImage", new { id = item.id })" 
     alt="Picture" style="width:150px" /> 
     </div> 
    } 
    else if (Excel, Word, PowerPoint) 
    { 
     don't know what to do 
    } 
} 

Controller 
public ActionResult MyImage(int id) 
{ 
    AttFile xFile = _db.AttFile.SingleOrDefault(x => (x.id == id)); 
    return File(xFile.attFile, xFile.MimeType); 
} 

我的想法是要表明,说的Excel文件,Word中的文件图像,PowerPoint文件,所以用户可以下载这个文件,或者可能是与文件名的链接,但我不知道如何。

任何人都可以帮助这个问题?

+1

你在问什么MIME类型的Word和Excel文件? http://filext.com/faq/office_mime_types.php – 2012-03-19 05:43:25

回答

0

您可以使用此代码,会弹出一个对话框,询问用户是否他要保存的文件或打开它:

public void DownLoad(string FName) 
    { 
     string path = FName; 
     System.IO.FileInfo file = new System.IO.FileInfo(path); 
     if (file.Exists) 
     { 
      Response.Clear(); 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
      Response.AddHeader("Content-Length", file.Length.ToString()); 
      Response.ContentType = "application/octet-stream"; 
      Response.WriteFile(file.FullName); 
      Response.End(); 

     } 
     else 
     { 
      Response.Write("This file does not exist."); 
     } 

    } 

行:Response.ContentType =“应用程序/八位字节流” ; 告诉下载哪种类型的文件。此类型用于docx文件。

相关问题