How can I save an HttpRequestMessage to file? - c#

I am using OWIN to self host an API within a WPF application. This is for a tool for a tester who doesn't want to have to enable IIS7.
Inside my Post action I want to be able to save the HTTP Request (ApiController.Request) to a file like I can with
System.Web.HttpRequest using SaveAs.
However OWIN doesn't provide HttpContext.Current(nor HttpContext.Current.Request), it's null, and I can't find any other way to get at a System.Web.HttpRequest.
Is there a way I can save a System.Net.Http.HttpRequestMessage to a file, or just read it as text from a Stream?

You can simply serialize any HttpRequestMessage and then store it wherever you want, e.g. in a text file. Later, you can read the file and deserialize its contents back into an HttpRequestMessage instance.

Related

Which data type in Asp Net Core C# can be used to catch Angular's blob object?

I have a BLOB object of an image in Angular 5, which I want to send it to backend via api. In backend which data type shall I use to capture the same. I am aware that in previous versions of Asp.Net there was 'HttpBaseType' but in core it is not available.
Therefore which datatype to be used for getting BLOB object of Angular in Asp.Net core c#?
Update: My use case is that I am drawing an image using canvas and getting its BLOB in Angular. Now I want this blob to be received at back-end. But I am not getting suitable type for the same. Here there is no involvement of file at all!
(I am not trying to upload the image/file)
Angular's blobs are merely just files (most of the time). You should be able to use ASP.NET Core's IFormFile to easily capture incoming data.
Blobs are merely files, if you are sending that request already, inspect your
browser and look at the raw request. If you do not want to use files then you'll have to create your own way of parsing the data.
I read some posts on it and it seems that Angular sends blobs attached as a file to a form. If you still don't want to do that then you can follow this post to encode it as base64 and do it yourself.
Here's how I would do it using IFormFile:
[HttpPost("upload_blob")]
public async Task<IActionResult> Post(IFormFile file)
{
Console.WriteLine(file.ContentType);
// process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok("some result");
}
Lastly, here are some posts you can look at:
MDN post about blobs
IFormFile example by Microsoft
IFormFile documentation
I hope I was of help, cheers!

How do I create a link to a file I have saved as a memory stream in a SQL table?

My web application (C# and ASP.Net) allows someone to upload a jpg/pdf file and save it as a memory stream inside a SQL table. This is what it looks like once written in the table:
0xFFD8FFE000104A4649.....
Now I want to provide this file back on my web interface through a link where the user can download this file. I have retrieved my file by converting the string above back to a byte array using
filedata = Encoding.ASCII.GetBytes(<string above>);
Then I called this:
string filename = "pic.jpg";
File.WriteAllBytes(filename, filedata);
But I have no clue how I should post this back to the user as a downloadable link on my web interface. Do I have to save this file to a temporary folder on my server or is there a way that I could invoke a call to render my file back as a picture where the user will be prompted to save the file or open it?
Thank you!
You will need to create a page / action / function that writes the bits back as the response to an HTTP request. Keep in mind that in doing so you will probably need to set the proper Content-Type header in the response according to what your file is.
So, you generate a link that calls your page / action / function. Then That sends the binary data back in an HTTP response. Something like
pic.jpg
If you some detail as to what framework you're using (MVC, WebForms, etc) then we can give more detailed examples.
Depending on how you want to present the image.
If you want to embed the image in the web return an link to the file with
string filename = Path.Combine(HttpRuntime.AppDomainAppPath, "Content/Images/pic.jpg");
File.WriteAllBytes(filename, filedata);
return filename;
then in the JavaScript side create an img element with thatsrc
Or if you want the user to download the file do what #squillman says in his answer

Posting file from mvc controller to webAPI service

a little background on our system, we have a mvc application that creates and displays forms, then posts these forms to a controller within the mvc application. it then does verification etc. etc.
I want to be able to use this to upload a file (currently using a post with the contoller pulling out the httppostedfilebase) have it send that file to a seperate application API which will pull the file information, store the information in the database, and save the file as something generic.
I have a method that can do all the pull apart/save file stuff, I have a controller that accepts my form post and gets all the relevant data including an httppostedfilebase. What I need is a way to send that file (which is not saved yet) over to our API.
We are hoping to avoid turning the file into a base64 string.
This is in c#.
Have you looked at this answer:
Web API: how to access multipart form values when using MultipartMemoryStreamProvider?
I think it will provide some ideas on how to handle streaming files in memory.
Solution we used:
using HttpPostedFileBase from the multipart form,
create byte array
stream file contents into the byte array
convert byte array to base64 string
add to json object along with file headers (name and extension)
post json object to api using HttpClient

How to respond to a GET file request?

I want some external client to search in one specific directory of my ASP.NET website for a specific file, and that file should be generated (and given to the client) when the user makes a request.
Is it possible? Which method should I use to program this response?
Yes, you could build an ASPX page for example that created the file on Page_Load and then set the byte[] of that file to the Response of the page.
... Page_Load(...)
{
// create the file
// set the response
Response.WriteFile(pathToNewFile);
}

How to write the response to a file in HTTP Handler

I have HTTP Handler where I want to log the response. I know there is option context.Request.SaveAs(filename... But I never tried this before. How can I use this, I mean someone can be more specific about file name?
Well, the documentation is pretty straight forward about this:
The [SaveAs] call specifies that the request be saved as a text file
in a directory where the ASP.NET process identity has been granted
write permissions, and that any header information included in the
request is included in the file.
You can thus simply save the entire HttpRequest (including the headers in a file). You can simply determine the path and a boolean indicating whether you want to incude the headers as well:
context.Request.SaveAs(#"c://myLogFile.log",true)

Categories