Neo*_*Neo 1 c# excel azure asp.net-mvc-4
我正在使用MVC4。我已经使用以下简单代码动态生成了Excel文件。我的托管在Azure上。
我创建了一个根路径,然后尝试保存该Excel文件。
问题是当我的ActionResult方法响应返回时,它会提供默认弹出窗口以打开文件,但文件名具有GUID而不是我提供的文件名。
Excel文件生成代码:
Microsoft.Office.Interop.Excel.Application xlApp =新的Microsoft.Office.Interop.Excel.Application();
// ...
//Save
LocalResource resource = RoleEnvironment.GetLocalResource("MyValue");
string tempPath = resource.RootPath + "DemoFile.xls";
return tempPath;
Run Code Online (Sandbox Code Playgroud)
tempPath返回类似的路径C:\AppData\Local\dftmp\Resources\11a2435c-998c-4fe8-aa55-8bb42455b4ca\directory\DemoFile.xls。
下载文件的弹出窗口不会给文件名作为DemoFile它gives some GUID为什么这样呢?
ActionResult 方法代码:
public ActionResult DownloadExcel() {
string path = ExcelGenerationCode(fileName);
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
return new FileStreamResult(s, "application/vnd.ms-excel");
}
Run Code Online (Sandbox Code Playgroud)
也试图给名字属性
public ActionResult DownloadExcel() {
string path = ExcelGenerationCode(fileName);
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
return new FileStreamResult(s, "application/vnd.ms-excel")
{
FileDownloadName = "myexcelFILE1.xls"
};
}
Run Code Online (Sandbox Code Playgroud)
给filename参数时出现以下错误。

我有足够的空间和内存。
您应该返回一个新的FileContentResult,并且操作必须返回FileResult而不是ActionResult例如:
public virtual FileResult DownloadFile(long fileId)
{
string path = ExcelGenerationCode(fileName);
return File(path, "application/vnd.ms-excel","donwload.xls");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8586 次 |
| 最近记录: |