【发布时间】:2013-07-26 07:01:05
【问题描述】:
我正在尝试将项目文件夹中存在的 PPT 下载到下载文件夹中。
System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Server.MapPath("~/Output/Document.pptx"));
DownloadPPT("Document.pptx", file);
这是下载PPT功能:
public void DownloadPPT(string fileName, System.IO.FileInfo file)
{
if (!file.Exists)
{
}
else
{
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the
// Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
//// add the header that specifies the file size, so that the browser
//// can show the download progress
//Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/vnd.ms-powerpoint";
// send the file stream to the client
Response.WriteFile(Server.MapPath("~/Output/Document.pptx"));
}
}
没有错误但没有下载ppt。
有人能说出我的代码有什么问题吗?
【问题讨论】:
-
如果你在一个 MVC 控制器动作中,你可以使用像
return File这样的 MVC Utility 方法并且返回类型为FileResult。 -
您是在控制器方法中调用您的第一个代码 sn-p 吗?如果是这样,您能否发布控制器方法的代码(或至少从该代码 sn-p 到末尾)?
-
不,我不在 ActionResult 中。我需要处理此代码本身的下载。不过感谢您的建议。
-
可能文件不存在? : ) 并且代码首先完成执行 if。你调试了吗?
-
文件存在。 else 部分条件被调用。
标签: c# asp.net-mvc