Home > Net >  General handler receives the uploaded files
General handler receives the uploaded files

Time:11-09

The receiving application is as follows:
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:

references for 1/f, the emperor god reply:
how to write a response in the foreache I haven't the second file save you return?

How many batch upload Files will ask how many times, you don't have to tube foreach, direct HttpPostedFile file=context. Request. The Files [0], each Request will only have a file in the Files

CodePudding user response:

Preached up, why not above failed to create the directory behind the response with the response. The writeEnd followed by filescount judgment doubt you have submitted an empty file,,,

File behind the saveas can also add Thread. Sleep (500).
  • Related