0

I was able using the following C# Web Api 2 code to fetch a file from a path and download the file. User is able to download abc.pdf by using this url http://localhost:60756/api/TipSheets/GetTipSheet?fileName=abc.pdf . I would like to make code change to open the file instead of downloading the file. How can I do that ?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using HttpGetAttribute = System.Web.Http.HttpGetAttribute;

namespace TestWebApi.Controllers
{
public class TipSheetsController : ApiController
{
    string tipSheetPath = @"C:\pdf\";
    string videoPath    = @"C:\video\";
    string clientIp;
    string clientIpSrc;

    public IHttpActionResult GetTipSheet(string fileName)
    {


        string ext = Path.GetExtension(fileName).ToLowerInvariant();
        string reqBook = (ext.Equals(".pdf")) ? tipSheetPath + fileName : videoPath + 
                          fileName;
        //string bookName = fileName;


        //converting Pdf file into bytes array
        try
        {
            var dataBytes = File.ReadAllBytes(reqBook);
            //adding bytes to memory stream 
            var dataStream = new MemoryStream(dataBytes);
            return new tipSheetResult(dataStream, Request, fileName);
        }
        catch (Exception)
        {

            throw;
        }
    }

}

public class tipSheetResult : IHttpActionResult
{
    MemoryStream bookStuff;
    string PdfFileName;
    HttpRequestMessage httpRequestMessage;
    HttpResponseMessage httpResponseMessage;
    public tipSheetResult(MemoryStream data, HttpRequestMessage request, string filename)
    {
        bookStuff = data;
        httpRequestMessage = request;
        PdfFileName = filename;
    }
    public System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
    {
        httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK);
        httpResponseMessage.Content = new StreamContent(bookStuff);            
        httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = PdfFileName;                        
        httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
        return System.Threading.Tasks.Task.FromResult(httpResponseMessage);
    }

   }
}
CK8
  • 347
  • 7
  • 22
  • _" I would like to make code change to open the file..."_ - open at server side? or download **and** open on client side? – vasily.sib Apr 08 '20 at 03:51
  • open on client side. the code already capable to download the file. Instead of ask user to open or save just open the file instead. – CK8 Apr 08 '20 at 03:59
  • There is no way (oh, thanks the gods) to forcefully do this from browser. There is an option to **open in browser** (see [this](https://stackoverflow.com/a/12086494/2716623) answer) but it depends on browser, that is used by your users. – vasily.sib Apr 08 '20 at 04:08
  • I change to inline, It open pdf without any issue in IE. But for mp4 file it still download and ask me to open or save. – CK8 Apr 08 '20 at 04:25

0 Answers0