I have api controller, which receives bytes, and sends bytes as answer.
public class RenderController : ApiController
{
[HttpPost]
public async Task<HttpResponseMessage> Post(string id)
{
var bytes = await Request.Content.ReadAsByteArrayAsync();
IoSys.Root = new IoSys {InputStream = new MemoryStream(bytes)};
var model = new DrawingChain().Load();
IoSys.Root.CloseIn();
using (var result = Request.CreateResponse(HttpStatusCode.OK))
{
using (var stream = new MemoryStream())
{
result.Content = new StreamContent(stream)
{
Headers = {ContentType = new MediaTypeHeaderValue("application/octet-stream")}
};
IoSys.Root = new IoSys {OutputStream = stream};
model.Save();
return result;
}
}
}
At line of returning a result, client is get a 500 error. I think, I work with streams not properly.
The objects are being disposed before the returns. Try to update your code in this way:
[HttpPost]
public async Task<HttpResponseMessage> Post(string id)
{
var bytes = await Request.Content.ReadAsByteArrayAsync();
IoSys.Root = new IoSys {InputStream = new MemoryStream(bytes)};
var model = new DrawingChain().Load();
IoSys.Root.CloseIn();
var stream = new MemoryStream() // Is it right here?
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
IoSys.Root = new IoSys {OutputStream = stream};
model.Save();
return result;
}
Please test it. According to your code, you are assigning a new MemoryStream() without content to the response.
using statement is equivalent of try..finally block. So essentially it's disposing the result object before returning.
Here's a relevant SO thread: When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?
Related
I have one memory stream object in my server-side, this object should be accessible in another party, which I call it a client or a consumer for my API.
In server-side I have a method like this (parameters.Save is related to a third-party library)
public MemoryStream GetSerializedParameters()
{
var parameters = GetParameters();
MemoryStream memory = new MemoryStream();
parameters.Save(memory);
return memory;
}
I'm thinking about sending this memory stream to a client with web API, so my action is something like this:
[HttpGet("parameters")]
public HttpResponseMessage GetParameters()
{
var stream = _server.GetSerializedParameters();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
return result;
}
I'm not sure if it is the right way and this implementation is correct because I am in trouble to consume it:
I do not know which method of httpClient I have to use: ReadAsStreamAsync() or anything else, because I could not find anything to work
Sure you can:
[HttpGet]
public async Task Get()
{
var randomString = "thisIsCool";
var randomStringBytes = Encoding.UTF8.GetBytes(randomString);
using (var ms = new MemoryStream(randomStringBytes))
{
await ms.CopyToAsync(this.Response.Body);
}
Based on my under standing below code may help you:
WEB API:
[HttpGet]
public HttpResponseMessage ReadToStream(HttpRequestMessage requestMessage)
{
var streamObj = _server.GetSerializedParameters();
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(streamObj);
requestMessage.RegisterForDispose(streamObj);
response.StatusCode = HttpStatusCode.OK;
return response;
}
Client Side
public async Task<string> DownloadFile(string guid)
{
var fileInfo = new FileInfo($"{guid}.txt");
var response = await _httpClient.GetAsync($"{url}/api/fileDownloadAPI?guid={guid}");
response.EnsureSuccessStatusCode();
await using var ms = await response.Content.ReadAsStreamAsync();
await using var fs = File.Create(fileInfo.FullName);
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fs);
return fileInfo.FullName;
}
I found the solution like this:
here is in server side:
[HttpGet("parameters")]
public IActionResult GetParameters()
{
var stream = _server.GetSerializedParameters();
stream.Seek(0, SeekOrigin.Begin);
return File(stream, MediaTypeNames.Text.Plain, "parameters.txt");
}
and here is in client-side:
public MemoryStream StoreParameters()
{
var request =new HttpRequestMessage
{
RequestUri = new Uri("https://localhost:44316/api/parameters"),
Method = HttpMethod.Get
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var result = _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
var ms = new MemoryStream();
result.Content.CopyToAsync(ms).Wait();
return result.IsSuccessStatusCode ? ms: null;
}
I have the following controller method which returns a byte array.
public async Task<HttpResponseMessage> Get()
{
var model = new byte[] { 1, 2, 3 };
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(model));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
I think this is an older way of implementing this functionality with web api. Is there a more "modern" version?
For example, is returning a Task<IHttpActionResult> the preferred way now? And if so, what would be the code to return the byte array from above?
As the comment pointed out. I dont think there is a new way to do this. But if you would like to return an IHttpActionResult instead, there is a base method that returns a ResponseMessageResult:
public IHttpActionResult Get()
{
var model = new byte[] { 1, 2, 3 };
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(new MemoryStream(model))
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return ResponseMessage(result);
}
Also, to return binary data in AspNetCore WebApi2 if anyone needs it:
[Route("api/v1/export/excel")]
[HttpGet]
public IActionResult GetAsExcel()
{
var exportStream = new MemoryStream();
_exportService.ExportAllToExcel(exportStream);
// Rewind the stream before we send it.
exportStream.Position = 0;
return new FileStreamResult(exportStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
I am struggling with being able to create a file with its data based on the byte array returned from the WebAPI. The following is my code for making the call to the web api
using (var http = new WebClient())
{
string url = string.Format("{0}api/FileUpload/FileServe?FileID=" + fileID, webApiUrl);
http.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
http.Headers[HttpRequestHeader.Authorization] = "Bearer " + authCookie.Value;
http.DownloadDataCompleted += Http_DownloadDataCompleted;
byte[] json = await http.DownloadDataTaskAsync(url);
}
The api code is
[HttpGet]
[Route("FileServe")]
[Authorize(Roles = "Admin,SuperAdmin,Contractor")]
public async Task<HttpResponseMessage> GetFile(int FileID)
{
using (var repo = new MBHDocRepository())
{
var file = await repo.GetSpecificFile(FileID);
if (file == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var stream = File.Open(file.PathLocator, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.FileType);
return response;
}
}
I receive a byte array as a response however am unable to create the corresponding file from that byte array. I have no idea how to convert the byte array into the relevant file type (such as jpg, or pdf based on file type in the web api). any help will be appreciated.
Alright so there are a few ways of solving your problem firstly, on the server side of things you can either simply send the content type and leave it at that or you can also send the complete filename which helps you even further.
I have removed the code that is specific to your stuff with basic test code, please just ignore that stuff and use it in terms of your code.
Some design notes here:
[HttpGet]
[Route("FileServe")]
[Authorize(Roles = "Admin,SuperAdmin,Contractor")]
public async Task<HttpResponseMessage> GetFileAsync(int FileID) //<-- If your method returns Task have it be named with Async in it
{
using (var repo = new MBHDocRepository())
{
var file = await repo.GetSpecificFile(FileID);
if (file == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var stream = File.Open(file.PathLocator, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.FileType);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName=Path.GetFileName(file.PathLocator)};
return response;
}
}
Your client side code has two options here:
static void Main(string[] args)
{
using (var http = new WebClient())
{
string url = string.Format("{0}api/FileUpload/FileServe?FileID={1}",webApiUrl, fileId);
http.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
http.Headers[HttpRequestHeader.Authorization] = "Bearer " + authCookie.Value;
var response = http.OpenRead(url);
var fs = new FileStream(String.Format(#"C:\Users\Bailey Miller\Downloads\{0}", GetName(http.ResponseHeaders)), FileMode.Create);
response.CopyTo(fs); <-- how to move the stream to the actual file, this is not perfect and there are a lot of better examples
fs.Flush();
fs.Close();
}
}
private static object GetName(WebHeaderCollection responseHeaders)
{
var c_type = responseHeaders.GetValues("Content-Type"); //<-- do a switch on this and return a really weird file name with the correct extension for the mime type.
var cd = responseHeaders.GetValues("Content-Disposition")[0].Replace("\"", ""); <-- this gets the attachment type and filename param, also removes illegal character " from filename if present
return cd.Substring(cd.IndexOf("=")+1); <-- extracts the file name
}
In my ASP.NET MVC project, I am trying to post image from Controller action method to API controller method. Plan is to leverage this API to other clients to use to upload images.
I am able to successfully hit the API Post method from Controller Action method, but am not able to pass the image object.
Here is my HomeController.cs Upload Action Method
[HttpPost]
public async Task<ActionResult> Upload(FormCollection formCollection)
{
var baseUri = "http://localhost/api/Process";
HttpPostedFileBase file = Request?.Files[0];
if (file == null || (file.ContentLength <= 0) || string.IsNullOrEmpty(file.FileName))
return new EmptyResult();
string fileName = file.FileName;
byte[] fileBytes = new byte[file.ContentLength];
HttpContent stringContent = new StringContent(fileName);
HttpContent fileStreamContent = new StreamContent(file.InputStream);
HttpContent bytesContent = new ByteArrayContent(fileBytes);
using (var formDataContent = new MultipartFormDataContent())
{
formDataContent.Add(stringContent, "fileName", fileName);
formDataContent.Add(fileStreamContent, "inputStream", fileName);
formDataContent.Add(bytesContent, "fileBytes", fileName);
using (var httpClient = new HttpClient())
{
formDataContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(baseUri, formDataContent);
var content = await response.Content.ReadAsStringAsync();
//Handle the response
}
}
return View("Result");
}
In my Process controller which is inheriting from ApiController, I have the following Post method
[HttpPost]
public async Task<string> Post([FromBody]MultipartFormDataContent formDataContent)
{
Task<string> imageContent = Request.Content.ReadAsStringAsync();
string body = imageContent.Result;
ImageResponse imageResponse = null;
//.................
//.................
return someValue
}
Here parameter formDataContent is always null and Request.Content.ReadAsStringAsync() is empty
Try sending from the Controller like this
//...other code removed for brevity
using (var form = new MultipartFormDataContent()) {
var stringContent = new StringContent("fileToUpload");
form.Add(stringContent, "fileToUpload");
var streamContent = new StreamContent(file.InputStream);
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
streamContent.Headers.ContentLength = file.ContentLength;
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "fileToUpload",
FileName = file.FileName
};
form.Add(streamContent);
using (var httpClient = new HttpClient()) {
var response = await httpClient.PostAsync(baseUri, form);
//...other code removed for brevity
}
}
And then process it in the ApiController by extracting the information sent ...
[HttpPost]
public async Task<IHttpActionResult> Post() {
var content = Request.Content;
//get file name from content disposition
var fileName = content.Headers.ContentDisposition.FileName;
//Get file stream from the request content
var fileStream = await content.ReadAsStreamAsync();
//...other code removed for brevity
return Ok();
}
Referenced this answer : Upload image using HttpClient
I'm attempting to return an image from a server route, but I'm getting one that is 0 bytes. I suspect it has something to do with how I'm using the MemoryStream. Here's my code:
[HttpGet]
[Route("edit")]
public async Task<HttpResponseMessage> Edit(int pdfFileId)
{
var pdf = await PdfFileModel.PdfDbOps.QueryAsync((p => p.Id == pdfFileId));
IEnumerable<Image> pdfPagesAsImages = PdfOperations.PdfToImages(pdf.Data, 500);
MemoryStream imageMemoryStream = new MemoryStream();
pdfPagesAsImages.First().Save(imageMemoryStream, ImageFormat.Png);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(imageMemoryStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = pdf.Filename,
DispositionType = "attachment"
};
return response;
}
Through debugging I have verified that the PdfToImages method is working and that imageMemoryStream gets filled with data from the line
pdfPagesAsImages.First().Save(imageMemoryStream, ImageFormat.Png);
However in running it, I receive an attachment that is properly named but is 0 bytes. What do I need to change in order to receive the whole file? I think it's something simple but I'm not sure what. Thanks in advance.
After writing to the MemoryStream, Flush it then set Position to 0:
imageMemoryStream.Flush();
imageMemoryStream.Position = 0;
You should rewind MemoryStream to beginning before passing it to response. But you'd better use PushStreamContent:
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new PushStreamContent(async (stream, content, context) =>
{
var pdf = await PdfFileModel.PdfDbOps.QueryAsync(p => p.Id == pdfFileId);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = pdf.Filename,
DispositionType = "attachment"
};
PdfOperations.PdfToImages(pdf.Data, 500).First().Save(stream, ImageFormat.Png);
}, "image/png");
return response;