我有一個通用處理程序,它設置爲從運行html5和javascript的ipad接收圖片。在通用處理程序中更改圖片的文件名
所有的代碼都是前後操作,簡短一件事。
我需要更改一般處理程序中處理圖片的文件名。
這裏是我的點擊按鈕的代碼 -
function sendPic() {
checkNetwork();
var myInput = document.getElementById('myFileInput');
var file = myInput.files[0];
var formData = new FormData();
formData.append('file', $('#myFileInput')[0].files[0]);
$.ajax({
type: 'post',
url: domainLocation + 'saveimage.ashx',
data: formData,
success: function (status) {
if (status != 'error') {
var my_path = "MediaUploader/" + status;
$("#myUploadedImg").attr("src", my_path);
}
},
processData: false,
contentType: false,
error: function() {
alert("Whoops something went wrong!");
}
});
}
這裏是通用的處理程序代碼 -
public void ProcessRequest(HttpContext context)
{
string fname;
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
file.SaveAs(@"\\fdsvr\mwfphotos\" + fname);
context.Response.ContentType = "text/plain";
context.Response.Write(fname);
}
}
}
我想在Ajax調用做數據塊的
data: {
"fileName" : fileName,
"formData" : formData
},
這個方向似乎落在處理程序 - 試圖從上下文獲取formData和文件名。
我會朝着正確的方向走嗎?我沒有在ajax調用中正確設置數據有效載荷嗎?一旦在處理程序中,我將如何將formData帶出上下文並在其中使用它?
謝謝
你是什麼意思**改變文件名**? – Rayon
我正在使用PHP,因此上傳文件處理稍有不同。但是,Mozilla文檔建議爲formData.append使用第三個參數來指定文件名。看到這個https://developer.mozilla.org/en-US/docs/Web/API/FormData/append – Tacticus
@Tacticus,完美的發現!發佈此作爲答案,可能會對其他人有所幫助。 – Rayon