So, I'm quite new to the ASP.NET MVC framework. What I need to do is make an API endpoint for a webserver which accepts a HTTP post request that has an image file uploaded to it.
I've looked around and I'm really not sure how to handle this. The closest I've come is this resource:
File Upload and Multipart MIME - ASP.NET Web API
Is this pretty much the only way to do it?
No, you can also send it across as a class that has a string property that contains the base64 encoded contents of the file. You then have to decode the base64 in the web api method (which is still a post) and then save or use the file bytes.
From the client's stand point, the most straight-forward way of doing it is multipart mime. But your question was about web api, and it can handle base64 in say, a json object just fine.
If you don't use multipart mime you'll need javascript that converts the upload into base64. That part of it is beyond the scope of your question.
But strictly speaking multipart is not the only way because I had such a javascript file upload library and that's what I used and it worked fine.
The reason why we used that component was because we wanted thumbnail upload preview of the images (preview first), then a submit button to actually submit to web api. javascript held the images in localstorage until the user hit submit (was a multiple image upload thing).
Related
I've been searching around the web for a long time but I couldn't find something specific about the best approach for this architecture issue.
I have an upload pattern that derives from Amazon which has two POST Api calls, the first sends the binary file returning its ID and then I have the second Api that sends the metadata combined with the location ID for this file where I can treat this in server side. I have this well implemented and managed inside my client.
However, now I want to GET this binary file together with the metadata, but I'm not sure if this two Api model is the best approach to handle inside the client. Maybe it has a content-type like Multipart, but I never know which metadata and how many files I will return. I also don't want to return in a Base64 because we are supposed to deliver contents more than 10mb and this would lack performance.
I was wondering about how Google, Amazon, Dropbox,etc deal with this with their APIs.
I am creating a MVC web application with Docusign REST API.
I am going to create actionlink in index view for each record witch downloads
document in the record's envelope.
I am planning to use call following HTTP request.
GET /v2/accounts/{accountId}/envelopes/{envelopeId}/documents/{documentId}
When I call this request, I get pop up where I can choose location and name of file for the document that I am downloading.
Is there any way for me to pass folder location and name of file as parameter and download the file without getting this pop up?
I don't think Docusign does not provide such API to download in this way.
Thank you in advance.
A Web Server can not access the client's computer. Imagine if Google (or any other site) could just start programs on your machine when you visit their site! So popup has nothing to do with DocuSign.
So using DS API, you will get pdf as byte[], once you got byte[] then its your code who has to write it to a File and that is not possible on a WEBServer as WebServer will not be able to access the CLient's computer to directly save them on the machine without showing that popup. If you run below code in Standalone Java Application or I think Java Applets can also do it, then you can use FileOutputStream to write byte[] into a file directly in the machine.
There would be an iOS App and the .NET WebApi which would require to sync large data or records.
We need to have in Memory Compression on both iOS and .NET WebApi which would compress and decompress the data and then further process.
On iOS App side, we have planned to use in Memory GZip compression using something like below-
http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/
http://www.deusty.com/2007/07/gzip-compressiondecompression.html
On .Net WebApi side, we have planned to use the GZip compression to be developed using-
https://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream%28v=vs.110%29.aspx
Now my question is-
Is it possible to use the GZipStream Class in .Net to compress the JSON string? In .Net data would be fetched from the database into models and then the models would be converted to JSON strings and then that string needs to be compressed before sending to device and same inverse process would be followed when receiving data from the iOS App.
Is there any other best approach then the above which we have planned? Any technical advice, relevant links or some sample code snippet would be very helpful.
The Solution:
Thanks to #puneet for leading this post to get a proper solution.
As my server application is a Asp.Net WebAPI and doesn't have any files (html, css, js etc.) to be transferred from the server to browsers like any traditional website. http://forums.asp.net/t/1771770.aspx
Therefore the Automatic IIS Compression would not work in my case, which can be enabled by following the steps given here- http://www.codeproject.com/Articles/186233/Utilize-gzip-compression-in-IIS
In my case both GZip Requests and Responses need to be handled and for this purpose seperate handlers would be required and in that case I am following these below SO posts-
For Compression: Compress HTTP GET Response
For Decompression: How do enable a .Net web-API to accept g-ziped posts
Another mentionable link- http://benfoster.io/blog/aspnet-web-api-compression
Is it possible to use the GZipStream Class in .Net to compress the JSON string? In .Net data would be fetched from the database into models and then the models would be converted to JSON strings and then that string needs to be compressed before sending to device
Yes it is possible by two ways.
Let the IIS handle it itself by enabling Dynamic Compression.
Write your own code to compress the response
Please follow the answer of this thread for both options.
I tried the second way (using the code given in referenced thread answer) and it worked for me very well.
and same inverse process would be followed when receiving data from the iOS App.
Yes, we can decompress the gZipped data.
[this blog post] (http://blog.kaliatech.com/2013/02/posting-compressed-json-content-to-asp-net-web-api-controller/) has the code for the same.
I haven't tried this one, but it seems fine and should work.
Till now, I was converting image to Base64String and then send it to Asp.NET server and convert it to image there. But recently, I came to know that HttpPostelFile is good method for it. I Google for some example but din't get anyone.
Please anyone help me to upload file from Android Device to Asp.NET Server using HttpPostedFile or any another good method.
I want it using Asp.NET Web Service.
I am new to .net. I created one website.I want to implement Zlib compression technique for compressing data in each every page, so that i can access open my website quickly.But i don't know where I have to write code for that and how to implement it. Can yo send me the sample application code or zip file.
If your talking about compressing web pages delivered from the server then gzip is used.
IIS6 Example