首页 新闻 搜索 专区 学院

.net一般处理程序接收上传文件问题

0
悬赏园豆:20 [待解决问题]

接收程序如下:
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。有遇到类似问题的有什么好的解决方法吗

Anoiy的主页 Anoiy | 初学一级 | 园豆:8
提问于:2020-08-04 14:40
< >
分享
所有回答(1)
0

这跟前端控件没关系,是因为你保存的时候,io进程有冲突

不知道风往哪儿吹 | 园豆:1551 (小虾三级) | 2020-08-04 16:11

有什么好的解决方法吗

支持(0) 反对(0) Anoiy | 园豆:8 (初学一级) | 2020-08-04 16:34

@Anoiy: 用玩file类的时候,或者打开一个文件的时候,记得关闭,知道不

支持(0) 反对(0) 不知道风往哪儿吹 | 园豆:1551 (小虾三级) | 2020-08-04 16:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册