input file標籤上傳圖片到伺服器

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   sp   java   檔案   資料   

引入jQuery庫
引入ajaxfileupload.js上傳外掛程式庫(這也是jQuery的一個外掛程式)
以ASP.NET為例

<input type="file" id="uploadfile" name="uploadfile"/>
<script type="text/javascript">
$("#uploadfile").change(function(){

$.ajaxFileUpload({
url: ‘../ajax/AjaxCallback.ashx‘,//處理上傳用的背景程式,可以是PHP,也可以是ASP等
secureuri: false,//非同步
fileElementId: ‘uploadfile‘,//上傳控制項ID
dataType: ‘json‘,//返回的資料資訊格式
success: function(data, status) {
if (data.code == ‘10000‘) {
alert("上傳成功");

} else {
alert("上傳失敗");
}
}, error: function(data, status, e) {
alert(e);
}
})

});

</script>
後台CS代碼

/// <summary>
/// 圖片上傳
/// </summary>
private void ImageUpload()
{
Response.ContentType = "text/html";//這裡一定要html
if (Request.Files.Count > 0)
{
HttpPostedFile file = Request.Files[0];
if (file.ContentLength > 0)
{
string suffix = file.FileName.Substring(file.FileName.LastIndexOf(‘.‘));//尾碼
if (".jpg.png.gif.jpeg".IndexOf(suffix.ToLower()) == -1)//檔案格式,這裡採用圖片格式說明
{
Response.Write("{\"msg\":\"檔案格式不正確!\",code:\"10001\"}");
return;
}

try
{
file.SaveAs(Server.MapPath("~/uploadfile/") + newName);
Response.Write("{\"msg\":\"" + newName + "\",code:\"10000\"}");
return;
}
catch (Exception ex)
{
Response.Write("{\"msg\":\"" + HttpUtility.HtmlEncode(ex.Message) + "\",code:\"10001\"}");
return;
}
}
Response.Write("{\"msg\":\"請選擇要上傳的檔案!\",code:\"10001\"}");
return;
}
Response.Write("{\"msg\":\"請選擇要上傳的檔案!\",code:\"10001\"}");
return;
}
http://www.cnblogs.com/linjiqin/p/3530848.html
http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html
http://www.phpletter.com/cn/Demo/AjaxFileUpload-Demo/

input file標籤上傳圖片到伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.