带上传文件的ajax保存,api.ajax上传文件,C#后台怎么接收存储文件

api.ajax({

url : GetWebOrgUrl() + '/AppInfo/UploadHandler.ashx’,

method : 'post',

dataType : 'json',

returnAll : false,

report : true,

data : {

files : {

"pic" : data

}

}

}, function(ret, err) {

if (ret) {

//上传中

if (ret.status == 0) {

if (ret.progress == 100) {

ProgressBar(ret.progress);

}

}

//上传完成

if (ret.status == 1) {

if (ret.body.msg.indexOf("成功") > -1) {

if (isEmpty(ret.body.path)) {

api.toast({

msg : '上传成功',

location : 'middle'

});

//关闭模块

UIActionProgress.close();

//回调图片

UploadingCallback(ret.body.path);

} else {

api.toast({

msg : '上传失败',

location : 'middle'

});

}

}

api.hideProgress();

}

} else {

api.alert({

msg : ('错误信息:' + err.msg + ';网络状态:' + err.statusCode),

location : 'middle'

});

api.hideProgress();

}

});

#region 上传图片处理

if (context.Request.Files["pic"] != null)

{

string path = context.Request.Files["pic"].ToString();

string[] array = path.Split(',');

HttpPostedFile file = context.Request.Files["pic"];

if (file.ContentLength <= maxFileSize)

{

//上传路径

if (System.IO.Directory.Exists(context.Server.MapPath(_urlpath)) == false)

{

System.IO.Directory.CreateDirectory(context.Server.MapPath(_urlpath));//不存在,创建文件夹

}

if (context.Request.Files.Count > 0)

{

for (int i = 0; i < context.Request.Files.Count; i++)

{

string fileName = file.FileName;//获取文件名(有些浏览器取到带路径的文件名)

fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);//获取文件名(带后缀名),去掉了前面的路径

fileName = fileName.Replace("#", "").Replace("@", "").Replace("%", "").Replace("&", "");//去掉图片名称中导致图片无法查看的特殊符号

if (System.IO.Directory.Exists(context.Server.MapPath(_urlpath)) == false)

{//判断该文件夹是否存在

System.IO.Directory.CreateDirectory(context.Server.MapPath(_urlpath));//不存在,创建文件夹

}

string newFileName = DateTime.Now.Ticks.ToString() + "_" + fileName;//新的文件名

string serverSavePath = _urlpath + newFileName;//得到服务器保存的路径

string fileAbsPath = context.Server.MapPath(serverSavePath);//绝对路径

file.SaveAs(fileAbsPath);

ret = "{\"status\": 200, \"msg\": \"上传文件成功!\", \"path\": \"" + serverSavePath + "\"}";

context.Response.Write(ret);

}

}

}

#endregion

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值