共計 2954 個字符,預計需要花費 8 分鐘才能閱讀完成。
這篇文章主要介紹了 Ajax 怎么上傳文件到服務器的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇 Ajax 怎么上傳文件到服務器文章都會有所收獲,下面我們一起來看看吧。
上傳文件是常要處理的事情,使用 ajaxFileUpload.js 處理比較方便,這里的 ajaxFileUpload.js 文件修改過的
Html 部分
input type= file id= fu_UploadFile name= fu_UploadFile value= title= 上傳附件
input type= button value= 上傳 quot;upload() /
JS 部分
function upload() { var path = document.getElementById( fu_UploadFile).value; if ($.trim(path) == ) { alert( 請選擇要上傳的文件 return; } console.log( test var result_msg =
$.ajaxFileUpload({ url: /UpFile.ashx , // 這里是服務器處理的代碼
type: post , secureuri: false, // 一般設置為 false
fileElementId: fu_UploadFile , // 上傳文件的 id、name 屬性名
dataType: json , // 返回值類型,一般設置為 json、application/json
data: {}, // 傳遞參數到服務器
success: function (data, status) { if (data.Result) { alert( 文件成功處理完成! + data.FileName);
} else { alert( 文件成功處理出錯!原因: + data.ErrorMessage);
}
}, error: function (data, status, e) {
alert( 錯誤:上傳組件錯誤,請檢察網絡!
}
});
}
服務器:
/// summary
/// UpFile 的摘要說明
/// /summary
public class UpFile : IHttpHandler
{ public void ProcessRequest(HttpContext context) {
context.Response.ContentType = text/plain
UpFileResult result = new UpFileResult(); try
{ HttpPostedFile _upfile = context.Request.Files[ fu_UploadFile if (_upfile == null)
{ throw new Exception( 請先選擇文件! } else
{ string fileName = _upfile.FileName;/* 獲取文件名: C:Documents and SettingsAdministrator 桌面 123.jpg*/
string suffix = fileName.Substring(fileName.LastIndexOf( .) + 1).ToLower();/* 獲取后綴名并轉為小寫: jpg*/
int bytes = _upfile.ContentLength;// 獲取文件的字節大小
//
if (!(suffix == jpg || suffix == gif || suffix == png || suffix == jpeg)) throw new Exception(只能上傳 JPE,GIF,PNG 文件 if (bytes 1024 * 1024*10) throw new Exception(文件最大只能傳 10M string newfileName = DateTime.Now.ToString( yyyyMMddHHmmss string fileDir = HttpContext.Current.Server.MapPath( ~/upfiles/ if (!Directory.Exists(fileDir))
{ Directory.CreateDirectory(fileDir);
}
result.FileName = fileName;
string saveDir = fileDir + newfileName + . + suffix;
result.FileURL = ~/upfiles/ + newfileName + . + suffix;
_upfile.SaveAs(result.FileURL);// 保存圖片
}
} catch (System.Exception ex)
{
result.Result = false;
result.ErrorMessage = ex.Message;
}
context.Response.Write(JsonHelper.SerializeObject(result));
} public bool IsReusable
{ get
{ return false;
}
} public class UpFileResult //: AshxCommonResult
{ public bool Result { get; set; } public string ErrorMessage { get; set; } public string FileName { get; set; } public string FileURL { get; set; }
}
ajaxfileupload.js
jQuery.extend({
handleError: function (s, xhr, status, e) { // If a local callback was specified, fire it
if (s.error) {
s.error.call(s.context || s, xhr, status, e);
} // Fire the global callback
if (s.global) {
(s.context ? jQuery(s.context) : jQuery.event).trigger(ajaxError , [xhr, s, e]);
}
},
createUploadIframe: function(id, uri)
{ //create frame
var frameId = jUploadFrame + id; var iframeHtml = iframe id= + frameId + name= + frameId + >關于“Ajax 怎么上傳文件到服務器”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Ajax 怎么上傳文件到服務器”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道。
正文完