Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 在MVC中下载docx文件_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 在MVC中下载docx文件

Asp.net mvc 4 在MVC中下载docx文件,asp.net-mvc-4,Asp.net Mvc 4,我已经上传了docx文件,然后我想下载docx文件,当我去下载时,出现了一些问题,问题如下图所示 我的代码是这样的 [HttpGet] public HttpResponseBase DownloadMapCyclo(Int32 CourseInfoCycloID = -1) { try { Response.AddHeader("Content-Dispositi

我已经上传了docx文件,然后我想下载docx文件,当我去下载时,出现了一些问题,问题如下图所示

我的代码是这样的

        [HttpGet]
        public HttpResponseBase DownloadMapCyclo(Int32 CourseInfoCycloID = -1)
        {
            try
            {
                Response.AddHeader("Content-Disposition", "Attachment;filename=file.docx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                Response.OutputStream.Write(byteArrays, 0, byteArrays.Length);
                return Response;
            }
            catch (Exception ex)
            {
                Response.Output.WriteLine("<h1>" + ex.Message + "</h1><br/><hr/>" + ex.InnerException.InnerException.Message);
                return Response;
            }
        }
[HttpGet]
公共HttpResponseBase下载MapCyclo(Int32 CourseInfoCycloID=-1)
{
尝试
{
AddHeader(“内容处置”、“附件;文件名=file.docx”);
Response.ContentType=“application/vnd.openxmlformats officedocument.wordprocessingml.document”;
Response.OutputStream.Write(bytearray,0,bytearray.Length);
返回响应;
}
捕获(例外情况除外)
{
Response.Output.WriteLine(“+ex.Message+”

“+ex.InnerException.InnerException.Message); 返回响应; } }

您对此下载有什么解决方案吗..谢谢

您的返回类型是
响应
将其更改为
文件
类型


文件(bytearray,“application/docx”,“PropsedChanges.docx”)

只需返回方法中的
ActionResult
并使用文件进行响应,而不是写入
响应
对象(从而将代码与活动HTTP上下文耦合)

public ActionResult DownloadMapCyclo(Int32 CourseInfoCycloID = -1)
{
    //...
    return File(byteArrays, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "file.docx");
}

1) 您确定返回的是文件而不是异常消息吗?2) 为什么不将
return File()
作为
ActionResult
而不是使用
Response
对象呢?嗨@David,好的,让我试试你正在返回响应,返回文件(内容,“application/docx”,“PropsedChanges.docx”);你好@David,你的答案是对的,你可以发布你的答案