Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [AllowAnonymous]
- [HttpGet]
- [Route("DownloadImage")]
- public HttpResponseMessage DownloadFile(string fileName)
- {
- string filePath = Properties.Settings.Default.Path;
- string fullPath = filePath + fileName;
- try
- {
- //Check if the file exists. If the file doesn't exist, throw a
- file not found exception
- if (!File.Exists(fullPath))
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- //Copy the source file stream to MemoryStream and close the filestream
- MemoryStream responseStream = new MemoryStream();
- Stream fileStream = File.Open(fullPath, FileMode.Open);
- fileStream.CopyTo(responseStream);
- fileStream.Close();
- responseStream.Position = 0;
- HttpResponseMessage response = new HttpResponseMessage();
- response.StatusCode = HttpStatusCode.OK;
- //Write the memory stream to HttpResponseMessage content
- response.Content = new StreamContent(responseStream);
- string contentDisposition = string.Concat("attachment;
- filename=", fileName);
- response.Content.Headers.ContentDisposition =
- ContentDispositionHeaderValue.Parse(contentDisposition);
- response.Content.Headers.ContentType =
- new MediaTypeHeaderValue("image/jpeg");
- return response;
- }
- catch
- {
- throw new
- HttpResponseException(HttpStatusCode.InternalServerError);
- }
- }
Add Comment
Please, Sign In to add comment