接收程序如下:
public class InventoryUpload : IHttpHandler
{
//服务器默认保存路径
private string strServerPath = ConfigurationManager.AppSettings["InventorPath"];//@"D:\InventorUpload\inventor";
private string strFilePath = HttpContext.Current.Request.ApplicationPath;
public InventoryUpload()
{
strFilePath = "\upload\inventory\";//(strFilePath == "" ? "~" : strFilePath) + "\upload\inventory\";
strServerPath = System.Web.HttpContext.Current.Server.MapPath(".") + "\upload\inventory\";
if (!Directory.Exists(strServerPath))
Directory.CreateDirectory(strServerPath);
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string queryString = "";
foreach (string key in context.Request.QueryString.AllKeys)
{
queryString = context.Request.QueryString[key];
}
string folder = strServerPath + queryString + "\\";
try
{
if (!string.IsNullOrWhiteSpace(queryString))
{
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
}
}
catch
{
context.Response.Write(JsonConvert.SerializeObject(new { code = false, msg = "创建目录失败", data = "" }));
}
// 获取 http提交上传的文件, 并改名保存
foreach (string key in context.Request.Files.AllKeys)
{
HttpPostedFile file = context.Request.Files[key];
string newFilename = DateTime.Now.Ticks + file.FileName.Substring(file.FileName.LastIndexOf('.'));
string fileUrl = folder + newFilename;
string filePath = Path.Combine(strFilePath + queryString + "\\", newFilename);
try
{ //文件保存并返回相对路径地址
file.SaveAs(folder + newFilename);
context.Response.Write(JsonConvert.SerializeObject(new { code = true, msg = "保存成功", data = new { fileNmae = newFilename, url = fileUrl, path = filePath.Replace("/", "") } }));
}
catch (Exception ex)
{
context.Response.Write(JsonConvert.SerializeObject(new { code = false, msg = ex.Message + ",Fail", data = "" }));
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
现在会造成一个问题,批量上传时总有一个问题保存不成功,提示进程被占用。上传控件用的是layui.upload。有遇到类似问题的有什么好的解决方法吗
这跟前端控件没关系,是因为你保存的时候,io进程有冲突
有什么好的解决方法吗
@Anoiy: 用玩file类的时候,或者打开一个文件的时候,记得关闭,知道不