How do you stream an Excel 2007 or Word 2007 file using asp.net and c#

I’m working on a web app and need to stream various files. I can do pdfs, images, and older Office documents. However, when I try to do with 2007 documents, it breaks. Here is my code:

    Response.Buffer = true;
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    switch (FileExtension.ToLower())
    {
        case "pdf":
            Response.ContentType = "application/pdf";
            break;
        case "doc":
            Response.ContentType = "application/msword";
            break;
        case "docx":
            Response.ContentType = "application/vnd.ms-word.document.12";
            break;
        case "xls":
            Response.ContentType = "application/vnd.ms-excel";
            break;
        case "xlsx":
            Response.ContentType = "application/vnd.ms-excel.12";
            break;
        default:
            Response.ContentType = "image/jpeg";
            break;
    }
    Response.BinaryWrite(buffer);

The error that I get is:

An invalid character was found in text content. Error processing resource 'http://DomainName/GetFile.aspx...

PK

Any suggestions?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

According to a brief web search, the correct mime types for word and excel are:

application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

http://www.bram.us/2007/05/25/office-2007-mime-types-for-iis/

Edit:

The following simplified sample works for me. It is different from yours in that it uses a generic handler instead of a web form (which is more appropriate for something like this anyway).

To test it, make sure there is an excel 2007 file named Book1.xlsx in the top level folder of the application.

DownloadSpreadsheet.ashx:

<%@ WebHandler Language="C#" Class="DownloadSpreadsheetHandler" %>

using System;
using System.Web;
using System.IO;

public class DownloadSpreadsheetHandler: IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        string path = context.Server.MapPath("~/Book1.xlsx");
        using (FileStream spreadsheet = File.OpenRead(path))
        {
            CopyStream(spreadsheet, context.Response.OutputStream);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

    private static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        while (true)
        {
            int read = input.Read(buffer, 0, buffer.Length);
            if (read <= 0)
                return;
            output.Write(buffer, 0, read);
        }
    }

}

Method 2

.doc


application/msword

.dot


application/msword

.docx


application/vnd.openxmlformats-officedocument.wordprocessingml.document

.dotx


application/vnd.openxmlformats-officedocument.wordprocessingml.template

.docm


application/vnd.ms-word.document.macroEnabled.12

.dotm


application/vnd.ms-word.template.macroEnabled.12

.xls


application/vnd.ms-excel

.xlt


application/vnd.ms-excel

.xla


application/vnd.ms-excel

.xlsx


application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.xltx


application/vnd.openxmlformats-officedocument.spreadsheetml.template

.xlsm


application/vnd.ms-excel.sheet.macroEnabled.12

.xltm


application/vnd.ms-excel.template.macroEnabled.12

.xlam


application/vnd.ms-excel.addin.macroEnabled.12

.xlsb


application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt


application/vnd.ms-powerpoint

.pot


application/vnd.ms-powerpoint

.pps


application/vnd.ms-powerpoint

.ppa


application/vnd.ms-powerpoint

.pptx


application/vnd.openxmlformats-officedocument.presentationml.presentation

.potx


application/vnd.openxmlformats-officedocument.presentationml.template

.ppsx


application/vnd.openxmlformats-officedocument.presentationml.slideshow

.ppam


application/vnd.ms-powerpoint.addin.macroEnabled.12

.pptm


application/vnd.ms-powerpoint.presentation.macroEnabled.12

.potm


application/vnd.ms-powerpoint.presentation.macroEnabled.12

.ppsm


application/vnd.ms-powerpoint.slideshow.macroEnabled.12

Method 3

For csv format files that we want Excel to be used to open, we are using: Response.ContentType = "application/msexcel";

Perhaps we get away with this because it is not a true xls file, however.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x