My Technical Notes

Thursday, 28 August 2014

Upload/Send File to User's Browser

The following method `SendFile` will send a file to the user's browser:


public static void SendFile(HttpResponse response, string fileName, byte[] fileBytes, string contentType = "application/octet-stream")
{
    response.BufferOutput = true;
    response.Buffer = true;
    response.ContentType = "Application/xlsx"; // google for correct mime type
    response.AddHeader("Content-Disposition", "attachment;filename= " + fileName);
    response.BinaryWrite(fileBytes);
    response.Flush();
    response.End();
}

No comments: