C# 如何显示用户ASP.NET C下载的Word、Excel和PowerPoint等文件#
我正在开发一个ASP.NET C#应用程序,有一个表,其中我定义了一个二进制列来存储文件(列名attFile),还保存了文件名、文件大小和Mimetype,大多数文件是JPG、PNG、PDF,我可以以用户可以预览图像的方式向用户显示这些文件,但使用Excel、Word PowerPoint等文件,我不知道如何展示它们,以便用户可以下载,我这样做的方式是C# 如何显示用户ASP.NET C下载的Word、Excel和PowerPoint等文件#,c#,asp.net-mvc,C#,Asp.net Mvc,我正在开发一个ASP.NET C#应用程序,有一个表,其中我定义了一个二进制列来存储文件(列名attFile),还保存了文件名、文件大小和Mimetype,大多数文件是JPG、PNG、PDF,我可以以用户可以预览图像的方式向用户显示这些文件,但使用Excel、Word PowerPoint等文件,我不知道如何展示它们,以便用户可以下载,我这样做的方式是 View @foreach (var item in Model) { if (item.MimeType == "applicatio
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 );
}
视图
@foreach(模型中的var项目)
{
如果(item.MimeType==“应用程序/pdf”)
{
}
else if(item.fotoMimeType==“image/jpeg”)
{
}
else if(Excel、Word、PowerPoint)
{
不知道该怎么办
}
}
控制器
公共操作结果MyImage(int id)
{
AttFile xFile=_db.AttFile.SingleOrDefault(x=>(x.id==id));
返回文件(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文件。您是否在询问Word和Excel文件的mime类型?