问题
I need to upload an image to a webservice using phonegaps Camera and File Transfer API.
So for example, on the Webservice have :
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string SaveImage(string imageData, string filename)
{
string success = "Nothing";
try
{
string filename = "text.png";
if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/devimg")))
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/devimg/"));
}
string path = HttpContext.Current.Server.MapPath("~/devimg/").ToString();
byte[] imgByte = Convert.FromBase64String(imageData);
using (var imgStream = new MemoryStream(imgByte, true))
{
Bitmap img = new Bitmap(imgStream);
Image i = (Image)img;
i.Save(path + "" + filename, ImageFormat.Png);
success = "Image created!!";
}
}
catch (Exception e)
{
success = e.ToString();
}
return success;
}
and in the app have:
navigator.camera.getPicture( function( imgdata ){
//Base 64 encoded image file or whatever
}, CameraError , {
quality: 50,
destinationType: navigator.camera.DestinationType.DATA_URL,
sourceType: navigator.camera.PictureSourceType.CAMERA,
encodingType: navigator.camera.EncodingType.JPEG,
saveToPhotoAlbum: true
});
The current webservice i have allows for a Base 64 encoded image to be passed into it which will then create the image and save it using the given file name. But it doesnt work if i try to access the webservice from the phonegap app.
Ive tried to access the webservice through ajax but just kept getting a bunch of error. is there a better way to upload the image to the server?
回答1:
As i cannot choose a comment as an answer, here is what my code looks like for future reference: Phonegap JS:
navigator.camera.getPicture( function( imgdata ){
var urlimg = "http:// *URL ADDRESS* /mobile.asmx/SaveImage";
var params = new Object();
params.otherinfo = "Whaterver"
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imgdata.substr(imgdata.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
setTimeout(function() {
ft.upload(imgdata, urlimg, function(){
alert("Success")
}, function(err){
alert("Error: " + JSON.stringify(err));
}, options );
}, 600);
}, function(err){
alert(err);
} , {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.CAMERA,
encodingType: navigator.camera.EncodingType.JPEG,
saveToPhotoAlbum: true
});
WebService
[WebMethod]
public void SaveImage()
{
if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/devimg"))){
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/devimg/"));
}
string path = HttpContext.Current.Server.MapPath("~/devimg/").ToString();
var Request = HttpContext.Current.Request;
if (Request.Files.Count > 0){
var file = Request.Files[0];
file.SaveAs( path + file.FileName);
}
}
回答2:
See here. for more information on setting parameters of file and making a call to your Web Service.
来源:https://stackoverflow.com/questions/21708797/phonegap-filetransfer-upload-image-to-webservice