layui 的上传图片 以及C# asmx 文件接收图片 存储本地
var uploadInst = upload.render({
elem: '#savetest' //绑定元素
, url: '/WebSever/AppTest.asmx/upimage' //上传接口
, data: { "TYPE": "update", "TEXT_ITEM": function () { return idexpage }, "PLATE_ID": PLATE_ID }
, done: function (res, index, upload) {
if (res.msg == 'ok') {
layer.open({
content: '修改完成',
icon: 1,
anim: 1,
yes: function (index, layero) {
layer.close(index); //如果设定了yes回调,需进行手工关闭
location.reload();
}
});
}
}
, error: function () {
//请求异常回调
}
});
后台 C#
[WebMethod(Description = "上传图片", EnableSession = true)]
public void upimage()
{
try
{
string pathf="";
//拿到用户唯一ID
string user = Session["USERID"].ToString();
//拿出来类型
string TYPE = HttpContext.Current.Request["TYPE"].ToString();
//板块 ID
string PLATE_ID = HttpContext.Current.Request["PLATE_ID"].ToString();
string TEXT_ITEM = "";
//修改 删除 用到的 ITEM
if (TYPE=="update") {
//修改的哪张照片
TEXT_ITEM = HttpContext.Current.Request["TEXT_ITEM"].ToString();
}
if (HttpContext.Current.Request.Files["file"] != null)
{
//获取流长度 文档的
int ls = HttpContext.Current.Request.Files["file"].ContentLength;
//随机名
string imgnamef = System.Guid.NewGuid().ToString();
//创建数组
byte[] bufferf = new byte[ls];
//接收 名称叫 file 的文件流
Stream sf = HttpContext.Current.Request.Files["file"].InputStream;
//读取流 存到数组中
sf.Read(bufferf, 0, bufferf.Length);
sf.Seek(0, SeekOrigin.Begin);
pathf = "/uploadFile/"+ user+"/"+ DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(HttpContext.Current.Server.MapPath(pathf)))
{
//没有目录就创建新的
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(pathf));
}
//后缀叠加
pathf += "/" + imgnamef +".jpg";
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(pathf), FileMode.Create, FileAccess.Write))
{
//存到本地
fs.Write(bufferf, 0, bufferf.Length);
//关闭流
fs.Close();
}
}
//存数据库
string str = HttpContext.Current.Request.Url.AbsoluteUri; //获得项目路径
//拿到这个 web的项目 路径 然后 截取 到 第三个 / 也就是 端口后面的 然后存到数据库
int Index = str.IndexOf("/", 7);
str = str.Substring(0, Index);
//真实路径
string src = str + pathf;
//新增 获取当前的 这个 ITEM是 多少了
if (TYPE=="add") {
string sql = string.Format("select TEXT_ITEM from VX_IMG_TEXT where PLATE_ID='{0}'", PLATE_ID);
DataTable data= bll.selectrows(sql);
if (data!=null && data.Rows.Count>=0) {
TEXT_ITEM = (data.Rows.Count+1).ToString();
}
}
int res = bll.GetLv(src, user, TYPE,PLATE_ID, TEXT_ITEM);
if (res != 1)
{
Context.Response.Write("异常");
}
string Json = "{\"code\": 1,\"msg\": \"ok\",\"data\": {\"src\": \""+ str + "\"}}";
//返回 完成
Context.Response.ContentType = "application/json";
Context.Response.Write(Json);
}
catch (Exception e)
{
Context.Response.ContentType = "application/json";
string Json = "{\"code\": 1,\"msg\": \""+ e.Message + "\",\"data\": {\"src\": \" \"}}";
Context.Response.Write(Json);
}
}