layui.use('upload', function () {
var upload = layui.upload;
upload.render({
elem: '#LAY_avatarUpload'
, url: setter.apiDomain + '/api/WxPay/UploadFileByStream_Head'
, choose: function (obj) {
//将每次选择的文件追加到文件队列
var files = obj.pushFile();
//预读本地文件,如果是多文件,则会遍历。(不支持ie8/9)
obj.preview(function (index, file, result) {
console.log(index); //得到文件索引
console.log(file); //得到文件对象
console.log(result); //得到文件base64编码,比如图片
//obj.resetFile(index, file, '123.jpg'); //重命名文件名,layui 2.3.0 开始新增
//这里还可以做一些 append 文件列表 DOM 的操作
//obj.upload(index, file); //对上传失败的单个文件重新上传,一般在某个事件中使用
//delete files[index]; //删除列表中对应的文件,一般在某个事件中使用
});
}
, done: function (index, res) {
//上传完毕回调
if (index.Data.Url != "") {
var url = index.Data.Url;
$(".UserHeadImgUrl").attr('src', setter.apiDomain + url);
}
var UserHeadImgUrl = $('.UserHeadImgUrl').attr("src");
var url = '/api/Userinformation/EditImg'; ----这个主要就是更改字段,就没粘上来
var parment = {
UserHeadImgUrl: UserHeadImgUrl
}
callpack = function ()
{
}
admin.ajax(url, parment, callpack)
}
, error: function (index) {
//请求异常回调
}
});
})
------------接口 UploadFileByStream_Head
public ResultInfo<UploadFileResponseModel> UploadFileByStream_Head()
{
var info = new ResultInfo<UploadFileResponseModel>();
try
{
#region 校验数据
if (!Request.Content.IsMimeMultipartContent())
{
info.Result = false;
info.StatusCode = (int)StatusCodeEnum.HttpRequestError;
info.State = StatusCodeEnum.HttpRequestError.ToString();
info.Msg = "不是有效的上传方式";
return info;
}
if (HttpContext.Current.Request.Files.Count <= 0)
{
info.Result = false;
info.StatusCode = (int)StatusCodeEnum.HttpRequestError;
info.State = StatusCodeEnum.HttpRequestError.ToString();
info.Msg = "没有上传文件";
return info;
}
#endregion
var resInfo = new UploadFileResponseModel();
HttpPostedFile file = HttpContext.Current.Request.Files[0];
var ext = Path.GetExtension(file.FileName);
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Funcs.GetRandom(1000000, 10000000) + ext;
var filesName = DateTime.Now.ToString("yyyyMMddHH");
var url = "/Upload/Consignor/Img/" + filesName + "/" + fileName;
var path = HttpContext.Current.Server.MapPath("/Upload/Consignor/Img/" + filesName + "/");
Directory.CreateDirectory(path);
var fullFileName = path + fileName;
file.SaveAs(fullFileName);
//返回数据
resInfo.FileName = fileName;
resInfo.ImagesType = "接口上传Stream";
resInfo.LocalPath = file.FileName;
resInfo.Url = url;
info.Result = true;
info.StatusCode = (int)StatusCodeEnum.Success;
info.State = StatusCodeEnum.Success.ToString();
info.Data = resInfo;
info.Msg = "上传成功";
return info;
}
catch (Exception ex)
{
string msg = "出现异常:" + ex.Message;
info.Result = false;
info.StatusCode = (int)StatusCodeEnum.Error;
info.State = StatusCodeEnum.Error.ToString();
info.Msg = msg;
return info;
}
};