cordova - Image upload using FileTransfer.Upload to ASP.NET REST API -
i trying implement android app using phonegap ionic framework. requirement upload image gallery remote server. achieve implemented asp.net rest api upload image server. (let assume url upload: http://xxxx.net/api/upload). inside rest api implemented uploadcontroller:webcontroller. has post() method looks below.
server code:
public string post() { httppostedfile myfile = httpcontext.current.request.files["recfile"]; if (myfile == null) return null; return utils.uploadtoserver(file); } my android code looks below:
$scope.uploadpictureex = function() { var options = new fileuploadoptions(); options.filekey="recfile"; options.filename=$scope.emplyeedata.imageuri.substr($scope.emplyeedata.imageuri.lastindexof('/')+1); options.mimetype="image/jpeg"; var params = new object(); params.value1 = "test"; params.value2 = "param"; options.params = params; options.chunkedmode = false; options.httpmethod = "post"; options.headers = { connection: "close" }; var ft = new filetransfer(); ft.upload($scope.emplyeedata.imageuri, "http://xxxx.net/api/upload/", win, fail, options); } thats it. when execute getting error saying error code = 3. can 1 please let me know how done? approach of implementing web api correct? doing thing wrong? thanks!!
have enabled cors in web api ?
install-package microsoft.aspnet.webapi.cors and in web api config:
public static class webapiconfig { public static void register(httpconfiguration config) { // new code config.enablecors(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } } and in controller can fine-grain control want enable:
[enablecors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")] public class testcontroller : apicontroller { // controller methods not shown... }
Comments
Post a Comment