user2851719
user2851719

Reputation:

Open new formats Microsoft Office without Office Online (web apps) in ASP NET MVC

I have problem with open new format documents Microsoft Office (docx, xlsx etc). I develop my project using ASP NET MVC.

I wrote new method:

[HttpGet]
[AllowAnonymous]
public ActionResult DownloadFileNew(Guid Id)
{
    FileTable fileTable = _DocumentService.GetFile(Id);

    HttpResponseBase response = ControllerContext.HttpContext.Response;
    response.ContentType = fileTable.ContentType;
    response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", fileTable.FileName));

    MemoryStream ms = new MemoryStream(fileTable.Data);
    FileStreamResult document = new FileStreamResult(ms, fileTable.ContentType);

    document.FileStream.Seek(0, SeekOrigin.Begin);
    document.FileStream.CopyTo(response.OutputStream, (int)document.FileStream.Length);

    response.Flush();
    return new EmptyResult();
}

But don't work with new formats, only old format (doc, xls etc).

When I try open document I take error message: "Excel web app cannot open this document ..."

Log file:

Upvotes: 0

Views: 234

Answers (1)

Kiru
Kiru

Reputation: 3565

Not sure what the ContentType you are using, but it has to be

For docx files

 Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

For doc files

Response.ContentType = "application/ms-word"

List of all mime types here

Upvotes: 1

Related Questions