此图片上传是由C# .NET技术写的一个完整的单张图片上传或多张图片上传的Demo,简单易懂,操作简单,功能强大。
获取源码方式:
第一种:打开微信,搜一搜"别打我女儿的主意"打开微信小程序,找到菜单栏,点击源码,进去就可以获得链接
第二种:可以给本文点赞、好评,然后发邮件到792166417@qq.com,如果有学习资料视频可以分享的话,可以捎带分享给我。
html页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8"/>
<script src="scripts/jquery-3.1.1.min.js"></script>
<script src="scripts/jquery.form.js"></script>
<script>
function upload(formid) {
$("#" + formid).ajaxSubmit({
url: "Handler.ashx?action=upload_img",
type: "POST",
dataType: "json",
clearForm: true,
resetForm: true,
success: function (e) {
if (e.status == 1) {
var temp = "";
for (var i = 0; i < e.files.length; i++) {
temp += "<image src='"+e.files[i].path+"' alt='"+e.files[i].filename+"' />";
}
$("#div_result").before(temp);
$("#div_result").text(JSON.stringify(e));
}
},
error: function (e) {
alert(e.statusText);
}
});
}
</script>
</head>
<body>
<form id="form1" method="POST" enctype="multipart/form-data" action="#" data-ajax="false">
<label for="file">多文件上传</label>
<input type="file" id="file" name="image" accept="image/png,image/jpg,image/bmp,image/jpeg" multiple="multiple" style="display: none" οnchange="return upload('form1');"/>
<div id="div_result"></div>
</form>
</body>
</html>
一般处理程序:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Test
{
/// <summary>
/// Handler 的摘要说明
/// </summary>
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string action = context.Request["action"];
switch (action)
{
case "upload_img":
upload_img(context);
break;
}
}
private void upload_img(HttpContext context)
{
HttpFileCollection files = context.Request.Files;
IList<HttpPostedFile> list = files.GetMultiple("image");
JObject json = new JObject();
json["status"] = 1;
JArray jfiles = new JArray();
//文件上传相对路径
string path = "/upload/";
//获取绝对路径
string mappath = context.Server.MapPath(path);
if (!Directory.Exists(mappath))
{
Directory.CreateDirectory(mappath);
}
foreach (HttpPostedFile file in list)
{
//获取扩展名
string ext = Path.GetExtension(file.FileName);
//生成新的文件名
string name = Guid.NewGuid().ToString("N");
//加上扩展名
string newfile = name + ext;
//保存上传的文件
file.SaveAs(mappath + newfile);
JToken jfile = new JObject();
jfile["ext"] = ext;
jfile["filename"] = newfile;
jfile["path"] = path + newfile;
jfiles.Add(jfile);
}
json["files"] = jfiles;
context.Response.Write(json.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}