ASP.NET MVCC#
Inheriting apiController to create WebAPI.
Previously, during development, one type of binary data was included in the requested body for POST.
curl --data-binary@"filename"
controller post (byte[]data)
I would like to POST two types of binary data (pem file and dat file).
How should curl and controller be implemented?
(Use as RESTAPI)
I would like to POST two types of binary data (pem file and dat file).
How should curl and controller be implemented?
I tried using the test code of the ASP.NET MVC5 app that I made on my Windows 10 development machine.
First curl side:
Curl with Windows 10 is the following command (-x127.0.0.1:8888 added to capture requests and responses in Fiddler, -k added to disable SSL validation) and
curl-x127.0.0.1:8888-k-F "[email protected]" -F "[email protected]" https://localhost:44365/File/MultipleUpload
The following request will be sent:
POST https://localhost:44365/File/MultipleUpload HTTP/1.1
Host:localhost:44365
User-Agent: curl/7.55.1
Accept: */*
Content-Length—112670
Expect—100-continue
Content-Type: multipart/form-data;boundary=----------------------------------------------------------------- 12dea397e7140308
--------------------------------------------------------------------
Content-Disposition: form-data; name="postedfiles"; filename="image1.jpg"
Content-Type: image/jpeg
...the contents of the file (omitted)...
--------------------------------------------------------------------
Content-Disposition: form-data; name="postedfiles"; filename="image2.jpg"
Content-Type: image/jpeg
...the contents of the file (omitted)...
------------------------------------------------------------------------
And ASP.NET MVC app side:
When the web server receives the above request, the MVC application model-binds files sent to the posted files argument of type IList<HttpPostedFileBase>
of the controller/action method specified in the curl command URL (File/MultipleUpload in the example above).Look at the image below.
For your reference, I will upload the sample code used for verification below.There are some things that have nothing to do with this thread, but can you tell the difference?Note that you are commenting out ValidateAntiForgeryToken because curl cannot send AntiForgeryToken.
Model
public class MultipleUploadModels
{
public string CustomField {get;set;}
public IList<HttpPostedFileBase>PostedFiles {get;set;}
}
Action Method
public ActionResult MultipleUpload()
{
return View();
}
[HttpPost]
// [ValidateAntiForgeryToken]
public ActionResult MultipleUpload (MultipleUploadModels model)
{
US>//#string result=";
string customFiled=model.CustomField;
IList<HttpPostedFileBase>postedFiles=model.PostedFiles;
// If the file is not selected with input type="file", postedFiles will not be null.
// postedFiles.Count will not be 0 (will be 1).Therefore, the control does not fly to else in the code below.
if (postedFiles!=null&&postedFiles.Count>0)
{
foreach (HttpPostedFileBase postedFile in postedFiles)
{
if (postedFile!=null&&postedFile.ContentLength>0)
{
// Retrieved the uploaded file name.Browser is IE
// where postedFile.FileName is full on the client side
// Use Path.GetFileName as it can be a path
string filename = Path.GetFileName (postedFile.FileName);
// US>Save Holder Physical Path\File Name
US>//#string path=Server.MapPath("~/UploadedFiles")+"\\"+filename;
// Save Uploaded Files
postedFile.SaveAs(path);
result+=filename+"("+postedFile.ContentType+")-"+
postedFile.ContentLength.ToString()+
" bytes Upload Complete <br/>";
}
}
result+="CustomField Sent:"+customFiled;
}
else
{
result="File upload failed";
}
if(Request.IsAjaxRequest())
{
return Content (result);
}
else
{
ViewBag.Result=result;
return View();
}
}
© 2024 OneMinuteCode. All rights reserved.