Shohel
Shohel

Reputation: 3934

Downloading docx file in MVC

I have uploaded the docx file, then i want to download docx file, when i go to download then some problem arise, problem are shown in picture below

enter image description here

enter image description here

My code look like this

        [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;
            }
        }

Do you have any solution for this downloading..thanks

Upvotes: 0

Views: 4648

Answers (2)

David
David

Reputation: 219047

Instead of writing to the Response object (and thereby coupling your code with an active HTTP context), simply return an ActionResult form the method and use the File() helper method to respond with a file.

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

Upvotes: 2

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3885

your Return Type is response change it to file type

File(byteArrays, "application/docx", "PropsedChanges.docx");

Upvotes: 0

Related Questions