public async Task <HttpResponseMessage> GetBlobDownload(string id) { // IMPORTANT: This must return HttpResponseMessage instead of IHttpActionResult Int32 blobId = Convert.ToInt32(id); try { var result = await _service.DownloadBlob(blobId); if (result == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } // Reset the stream position; otherwise, download will not work result.BlobStream.Position = 0; // Create response message with blob stream as its content var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(result.BlobStream) }; // Set content headers message.Content.Headers.ContentLength = result.BlobLength; message.Content.Headers.ContentType = new MediaTypeHeaderValue(result.BlobContentType); message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlDecode(result.BlobFileName), Size = result.BlobLength }; return(message); } catch (Exception ex) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(ex.Message) }); } }
public async Task <IHttpActionResult> GetFile(string blobName) { try { var result = await _service.DownloadBlob(blobName); if (result == null) { return(NotFound()); } // Reset the stream position; otherwise, download will not work result.BlobStream.Position = 0; // Create response message with blob stream as its content var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(result.BlobStream) }; // Set content headers message.Content.Headers.ContentLength = result.BlobLength; message.Content.Headers.ContentType = new MediaTypeHeaderValue(result.BlobContentType); message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlDecode(result.BlobFileName), Size = result.BlobLength }; return(ResponseMessage(message)); } catch (Exception ex) { return(ResponseMessage(new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(ex.Message) })); } }
/// <summary> /// Downloads a blob file. /// </summary> /// <param name="blobId">The ID of the blob.</param> /// <returns></returns> public async Task <HttpResponseMessage> GetBlobDownload(string voucherCode) { var voucher = db.Vouchers.Where(i => i.VoucherCode == voucherCode).FirstOrDefault(); var blob = voucher.VoucherFileId; if (voucher == null) { return(new HttpResponseMessage(HttpStatusCode.Forbidden)); } if (blob.FileId != voucher.VoucherFileId.FileId) { return(new HttpResponseMessage(HttpStatusCode.Forbidden)); } // IMPORTANT: This must return HttpResponseMessage instead of IHttpActionResult try { var result = await _service.DownloadBlob(blob.FileId); if (result == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } // Reset the stream position; otherwise, download will not work result.BlobStream.Position = 0; // Create response message with blob stream as its content var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(result.BlobStream) }; // Set content headers message.Content.Headers.ContentLength = result.BlobLength; message.Content.Headers.ContentType = new MediaTypeHeaderValue(result.BlobContentType); message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlDecode(result.BlobFileName), Size = result.BlobLength }; voucher.VoucherRedeemed = true; voucher.VoucherRedemptionCounter++; voucher.VoucherRedemptionDate = DateTime.Now; await db.SaveChangesAsync(); return(message); } catch (Exception ex) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(ex.Message) }); } }