一个 ashx的 上传,代码示例

-------------------------js

function ajaxUploadImg(btn) {

var button = btn, interval;
new AjaxUpload(button, {
action: 'upload.ashx',
data: {},
responseType: 'json',
name: 'myfile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF)$/.test(ext))) {
nerror(jsT.T("图片格式不对"));
return false;
}
},
onComplete: function (file, response) {
ConsumableModal.find(".img").attr("src", response.imgurl);
ConsumableModal.find(".hidImgName").val(response.imgurl);
}
});
}

--------------------------ashx

public class Upload : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
string ossurl = GlobalOSS.ossurl;
HttpFileCollection files = context.Request.Files;
string msg = string.Empty;
string error = string.Empty;
string imgurl;
if (files.Count > 0)
{
string basepath = AppDomain.CurrentDomain.BaseDirectory;
string path = System.IO.Path.Combine(basepath, "CumScore/AssetImages");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

string newfilename = System.IO.Path.GetFileNameWithoutExtension(files[0].FileName) + "_" + Guid.NewGuid().ToString() + System.IO.Path.GetExtension(files[0].FileName);
string fullpath = System.IO.Path.Combine(path, newfilename);
files[0].SaveAs(fullpath);
if (File.Exists(fullpath))
{
GlobalOSS.DoOss(fullpath, "oas/" + newfilename);
if (File.Exists(fullpath))
File.Delete(fullpath);
}
msg = "";
// imgurl = "/Fixed/FixImages/" + newfilename;
imgurl = ossurl + newfilename;
string res = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + imgurl + "'}";
context.Response.Write(res);

}
}

public bool IsReusable
{
get
{
return false;
}
}
}

 

-----------------------------

返回 的 imgurl  可能是这样子的:

https://ttt1-oss-euapp-test.oss-cn-shenzhen.aliyuncs.com/oas/Dingtalk_20241-2_38f9b6e9-4910-4e47-8bd9-4a4f20325d79.jpg

 

posted @ 2024-12-18 11:31  以函  阅读(4)  评论(0编辑  收藏  举报