Jq_input file标签上传图片到服务器

引入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/
posted @   彪悍的代码不需要注释  阅读(7266)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
39
0
点击右上角即可分享
微信分享提示