gpt4 book ai didi

jQuery ajax 将 jpg 图像发布到 .net web 服务。图像结果已损坏

转载 作者:行者123 更新时间:2023-12-01 01:37:22 24 4
gpt4 key购买 nike

我有一个phonegap jQuery 应用程序,可以打开相机并拍照。

然后我将此图片发布到我已编码的 .net Web 服务。

我无法使用phonegap FileTransfer,因为Bada os 不支持此类功能,但这是一项要求。

我已经成功从phonegap FileSystem API加载图像,我已将其附加到.ajax类型:post中,我什至从.net端收到了它,但是当.net将图像保存到服务器中时,图像结果已损坏

->> 这是一个正确的 Base64 编码文件,但有这个 header “data:image/jpeg;base64,/9j/4AAQ.....=”

如何去掉这个标题?我应该从 .net 还是 ajax 中修剪它?

任何帮助将不胜感激。

这是我的代码:

//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");
}
});
}

在我的 .net Web 服务上,这是被调用的方法:

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();
}

最佳答案

好的,我成功了。问题是,当我们使用 $.ajax() 时,结果必须作为文本适合 html,因此例如您可以将其直接放入标签中

<textarea> background-image: data:data:image/jpeg;base64,/9j/4AAQ.....=</textarea>

但是当您尝试 Convert.FromBase64String(thestring) 时,即使 .net 也会告诉您“Base64 字符串中的字符无效”。

总之,保持 $.ajax 不变,我这样修改了我的 .net 函数:

 //Convert input stream into string
StreamReader postReader = new StreamReader(inputStreamFromWebService);
string base64String = postReader.ReadToEnd();
if (base64String.StartsWith("data:"))
{
//remove unwanted ajax header
int indexOfBase64String = base64String.IndexOf(",") + 1;
int lenghtOfBase64String = base64String.Length - indexOfBase64String;
base64String = base64String.Substring(indexOfBase64String, lenghtOfBase64String);
}

//Convert from base64 string to byte[]
byte[] byteFromString;
byteFromString = Convert.FromBase64String(base64String);
MemoryStream stream = new MemoryStream(byteFromString);

System.IO.FileStream outFile;
outFile = new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
outFile.Write(byteFromString, 0, byteFromString.Length);
outFile.Close();

关于jQuery ajax 将 jpg 图像发布到 .net web 服务。图像结果已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9951581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com