jQuery ajax post of jpg image to .net webservice. Image results corrupted
Posted
by
sosergio
on Stack Overflow
See other posts from Stack Overflow
or by sosergio
Published on 2012-03-30T23:27:48Z
Indexed on
2012/03/30
23:28 UTC
Read the original article
Hit count: 255
I have a phonegap jquery app that opens the camera and take a picture.
I then POST this picture to a .net webservice, wich I've coded.
I can't use phonegap FileTransfer because such isn't supported by Bada os, wich is a requirement.
I believe I've successfully loaded the image from phonegap FileSystem API, I've attached it into an .ajax type:post, I've even received it from .net side, but when .net save the image into the server, the image results corrupted.
It seems to me that two sides of the communication have different data type. Has anyone experience in this?
Any help will be appreciated.
This is my code:
//PHONEGAP CAMERA ACCESS (summed up)
navigator.camera.getPicture(onGetPictureSuccess, onGetPictureFail, { quality: 50, destinationType:Camera.DestinationType.FILE_URI });
window.resolveLocalFileSystemURI(imageURI, onResolveFileSystemURISuccess, onResolveFileSystemURIError);
fileEntry.file(gotFileSuccess, gotFileError);
new FileReader().readAsDataURL(file);
//UPLOAD FILE
function onDataReadSuccess(evt) {
var image_data = evt.target.result;
var filename = unique_id();
var filext = "jpg";
$.ajax({
type : 'POST',
url : SERVICE_BASE_URL+"/fotos/"+filename+"?ext="+filext,
cache: false,
timeout: 100000,
processData: false,
data: image_data,
contentType: 'image/jpeg',
success : function(data) {
console.log("Data Uploaded with success. Message: "+ data);
$.mobile.hidePageLoadingMsg();
$.mobile.changePage("ok.html");
}
});
}
On my .net Web Service this is the method that gets invoked:
public string FotoSave(string filename, string extension, Stream fileContent)
{
string filePath = HttpContext.Current.Server.MapPath("~/foto_data/") + "\\" + filename;
FileStream writeStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
© Stack Overflow or respective owner