1

My Current code is below, which only downloads the files. How to view all files types in a browser?

[HttpGet]
    //[NoCacheHeader()]
    [Route("api/image/files")]
    public HttpResponseMessage GFiles(string ImageName)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK);
        var path = "~/Image/" + ImageName;

            path = System.Web.Hosting.HostingEnvironment.MapPath(path);
            var ext = System.IO.Path.GetExtension(path);

            var contents = System.IO.File.ReadAllBytes(path);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(contents);

            response.Content = new StreamContent(ms);

            //

            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = ImageName;
            //

            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("Image/" + ext);
            return response;

    }
2
  • By not telling it it’s an attachment in Content Disposition header you can suggest opening it inline. The browser can still do whatever it wants Commented Jun 15, 2018 at 5:56
  • I got answer for pdf and image file: for pdf write "application/pdf" in mediaTypeHeaderValue. And for image write "images/jpeg" for jpg files. MIME type should be given in MediaTypeHeaderValue. But for docx I'm still searching the solution Commented Jun 15, 2018 at 10:48

2 Answers 2

6

I found solution for only pdf and images. Just add MIME type inyour MediaTypeHeaderValue and make sure ContentDispositionHeaderValue to be "inline"

 response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");

for pdf:

response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");

for images:

response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("images/jpeg");

vary your MIME according to image types. for microsoft documents use it's mime type in mediaheadervalue and then install offline google docs extension in google chrome

Sign up to request clarification or add additional context in comments.

Comments

0

Just to add to accepted answer slightly: Attachment means download, Inline means open. But browser also needs to know what type it's opening to prevent download.

Also, you can change between whether you're getting the content from another web service, or via stream using commented line.

So if you wanted to open in browser a pdf you were getting from yet another webservice, it would look like:

var result = new HttpResponseMessage(HttpStatusCode.OK)
{
    //Content = new ByteArrayContent(stream.ToArray())
    Content = new ByteArrayContent(response.RawBytes)
};

result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline") //attachment
    {
        FileName = fileName
    };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); //images/jpeg

return result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.