I am trying to figure out a way to to upload an image from the mobile phone to a remote server using Icenium+Cordova (mobile) and ASP.NET.
I did try to use FileTransfer() command while providing a remote webservice address but without success. I am using Icenium simulator and Visual Studio to test the code locally.
What I need is a code example of the mobile (Javascript) and Server (.NET) side to support that image upload communication. Thanks.
The code that I am currently using:
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="image_file"; // recFile
var imagefilename = Number(new Date())+".png";
options.fileName=imagefilename;
options.mimeType= "text/plain";
options.chunkedMode = false;
params = {
val1: "some value",
val2: "some other value"
};
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI,"http://127.0.0.1:1691/ImageWebService.asmx/SaveImage", success, fail, options);
}
On the server side:
[WebMethod]
[ScriptMethod]
public string SaveImage()
{
try
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
if (file == null)
return "0";
HttpPostedFile file =
HttpContext.Current.Request.Files[0];
string targetFilePath = "c:\\" + file.FileName;
file.SaveAs(targetFilePath);
}
catch (Exception ex)
{
}
return "1";
}
I also have:
<access origin="*" />
In the config.xml for cordova.
Note: I tested the webservice for image upload using the standard file upload control using "Advanced Rest Client" and it returned 200 OK.
Other than that, I'm stuck and can find a way to successfully upload am image to the remote server. I am open to using other method, but I think that using the native Cordova FileTransfer() is the safer way to do that if I want the best comparability.