接收程式如下:
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批量上傳時,第一次上傳會提示有一兩個檔案行程被占用,無法保存。有遇到過這種問題的嗎。有沒有好的解決方法??
uj5u.com熱心網友回復:
在foreache 里里面怎么寫了response 我第二個檔案還沒保存你就回傳了?uj5u.com熱心網友回復:
批量上傳多少個檔案會請求多少次。你不用管foreach,直接HttpPostedFile file = context.Request.Files[0]。每次請求Files里只會有一個檔案
uj5u.com熱心網友回復:
為什么不一次傳上去呢。response 上面創建目錄失敗 后面加上 response.writeEnd 后面加上filescount的判斷 懷疑你提交了空檔案。。。檔案saveas的后面還可以加上 Thread.Sleep(500)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/16181.html
標籤:ASP.NET
上一篇:定時任務調度自動提醒企業微信
下一篇:VB.NET
