![](/img/trans.png)
[英]How to read a PDF,Word and Excel files from LinkButton click ? + Asp.Net + C#
[英]How to present files like Word, Excel and PowerPoint to be downloaded by the user ASP.NET C#
我正在使用ASP.NET C#應用程序,有一個表,其中定義了一個二進制列來存儲文件(列名attFile),還保存了文件名,文件大小和Mimetype,大多數文件為JPG,PNG, PDF,對於以預覽圖像的方式向用戶顯示這些文件沒有問題,但是對於Excel,Word PowerPoint這樣的文件,我不知道如何顯示它們以便用戶可以下載它我正在做的是
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文件,以便用戶可以下載此文件或帶有文件名的鏈接,但我不知道如何。
有人可以幫忙嗎?
您可以使用此代碼,將出現一個彈出對話框,詢問用戶是否要保存文件或打開文件:
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 =“ application / octet-stream”; 告知要下載的文件類型。 此類型用於docx文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.