asp.net mvc - Use Ajax.Beginform to send multipart/form-data -
i trying post multipart/form-data web api in view. code of view is:
@{ viewbag.title = "upload"; } <h2>upload image</h2> <div> @using (ajax.beginform(null, null, null, new ajaxoptions { httpmethod = "post", url = "http://localhost:59650/api/ingestion/upload", onsuccess = "onsuccess", onfailure = "onfailure", onbegin = "onbegin" }, new { enctype = "multipart/form-data" })) { <div> <ol> <li> <input type="file" name="fileupload" /> </li> <li> @html.label("sheetname: ") @html.textbox("sheetname") </li> <li> @html.label("columns: ") @html.textbox("columns") </li> <li> @html.label("file name: ") @html.textbox("filename", viewdata["filename"]) </li> </ol> </div> <div> <input type="submit" name="upload_btn" value="upload" /> </div> } </div> @section scripts { <script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript"> function onbegin(xhr) { xhr.setrequestheader("content-type", "multipart/form-data") } function onsuccess(response) { alert("upload successfully"); //todo:change location window.location.href = "http://localhost:59650/"; } function onfailure(response) { alert("upload failed, please try again"); //todo:change location window.location.href = "http://localhost:59650/ingestion/uploading"; } </script> }
i make content type multipart/form-data in http attributes in beginform helper method in ajax call. however, in api method, got exception saying:
"invalid 'httpcontent' instance provided. not have 'multipart' content-type header 'boundary' parameter.\r\nparameter name: content"
my web api in controller:
[routeprefix("api/ingestion")] public class valuescontroller : apicontroller { queueclient senderclient; [authorize] /// <summary> /// post request encrypt , upload file onto azure blob storage. /// receives value form-data type different values such column, user id, key etc. /// sends uploaded file details etl service. /// </summary> /// <returns>ihttpactionresult either ok or badrequest</returns> [route("upload")] [httppost] public async task<ihttpactionresult> postformdata() { var blob = new blobuploadmodel(); try { multipartformdatastreamprovider provider = new multipartformdatastreamprovider(path.gettemppath()); //i got exception here await request.content.readasmultipartasync(provider); //do sth ... } catch (exception ex) { return internalservererror(ex); } return ok(blob); } }
i confused why getting exception. there other ways send form specific url , handle response accordingly rather using ajax.beginform method?
Comments
Post a Comment