Public class InventoryUpload: IHttpHandler
{
//server default save the path
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. The Exists (strServerPath))
Directory. CreateDirectory (strServerPath);
}
Public void the ProcessRequest (HttpContext context)
{
The context. The Response. ContentType="text/plain";
String the queryString="";
Foreach (string key in context. The Request. The QueryString. AllKeys)
{
[key] the queryString=context. Request. The queryString.
}
String folder=strServerPath + the queryString + "\ ";
Try
{
if (! String. IsNullOrWhiteSpace (the queryString))
{
if (! Directory. The Exists (folder))
Directory. CreateDirectory (folder);
}
}
Catch
{
The context. The Response. The Write (JsonConvert SerializeObject (new {code=false, MSG="failed to create the directory", datahttps://bbs.csdn.net/topics/=""}));
}
//get HTTP submit to upload documents, and changed his name to save
Foreach (string key in the context, the Request Files. AllKeys)
{
The HttpPostedFile file=context. Request. Files [key];
String newFilename=DateTime. Now. Ticks + file. The FileName. The Substring (file. The FileName. LastIndexOf ('. '));
String fileUrl=+ newFilename folder;
String filePath=Path.Com bine (strFilePath + + "\ " the queryString, newFilename);
Try
{//file and return the relative path to address
File. The SaveAs (folder + newFilename);
The context. The Response. The Write (JsonConvert SerializeObject (new {code=true, MSG="successfully saved", data=https://bbs.csdn.net/topics/new {fileNmae=newFilename, url=fileUrl, path=filePath. Replace ("/", "")}}));
}
The catch (Exception ex)
{
Context. The Response. The Write (JsonConvert SerializeObject (new {code=false, MSG=ex. Message +, "Fail", datahttps://bbs.csdn.net/topics/=""}));
}
}
}
Public bool IsReusable
{
The get
{
return false;
}
}
}
Using layui. Upload batch upload, for the first time the upload will be prompted to process occupied one or two files, unable to save, have encountered this problem, do you have any good solution??
CodePudding user response:
How to write a response in foreache in the second file I haven't save your return?CodePudding user response: