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);
}
}
}