public void ProcessRequest (HttpContext context) {context. Response.ContentType = "Text/plain"; StringBuilder sb = new StringBuilder (); string resultcode = null; string errormsg = null; string paths = ""; String names = ""; string title = ""; String fileguid = ""; if (context. Request.Files.Count = = 0) {ResultCode = "1003"; ErrorMsg = "The upload file is empty!" "; Sb. Append ("{\" resultcode\ ":" + "\" "+ ResultCode +" \ "); Sb. Append (","); Sb. Append ("\" errormsg\ ":" + "\" "+ errormsg +" \ "); Sb. Append (","); Sb. Append ("\" Datas\ ":" + "{"); Sb. Append ("\" filepath\ ":" + "\" "+ Paths +" \ "); Sb. Append (","); Sb. Append ("\" filename\ ":" + "\" "+ names +" \ "); Sb. Append (","); Sb. Append ("\" filetitle\ ":" + "\" "+ title +" \ "); Sb. Append (","); Sb. Append ("\" fileguid\ ":" + "\" "+ Fileguid +" \ "); Sb. Append ("}"); Sb. Append ("}"); } else {String guid = System.Guid.NewGuid (). ToString ("N"); String dir = context. Server.MapPath ("~/taskmobile/files/") + GUID; if (! System.IO.Directory.Exists (dir)) {System.IO.Directory.CreateDirectory (dir); } for (int i = 0; I < context. Request.Files.Count; i++) {//Gets the object of the uploaded file httppostedfile img = context. Request.files[i]; Gets the name of the uploaded file string s = img. FileName; var strfileextension = s.substring (S.lastindexof ('. ') + 1, s.length-s.lastindexof ('. ')-1). ToUpper (); String allowextension = system.configuration.configurationmanager.appsettings["ALLOWUPLOADFIletype "]; if (allowextension. IndexOf (strfileextension) >= 0) {int len = S.lastindexof (".")-S.lastindexo F ("\ \")-1; String fileName = S.substring (s.lastindexof ("\ \") + 1, Len); String fullName = S.substring (s.lastindexof ("\ \") + 1); FileName = Filename.replace (",", ""); FullName = Fullname.replace (",", ""); FullName = Fullname.replace (":", ""); Intercept to get the name of the uploaded file (ie upload will also attach the absolute path, here only to get the name of the file)//string str = System.Guid.NewGuid (). ToString ("N") + s.substring (S.lastindexof (".")-1); S.substring (s.lastindexof ("\ \") + 1); String path = dir + "\ \" + FullName; Save file IMG. SaveAs (path); paths = Httpruntime.appdomainappvirtualpath + "/taskmobile/files/" + GUID + "/" + fullName; title = "/taskmobile/files/" + GUID + "/" + fullName; names = FullName; Attachment capital (in kilobytes) int size = img. contentlength/1024; Save the attachment to the file table Fileguid = new Ndfiles (). AddFiles (Names, "", Strfileextension, Ndfiles.getfiletype (strfileextension), size. ToString (), paths, context. User.Identity.Name); ResultCode = "1000"; Sb. Append ("{\" resultcode\ ":" + "\" "+ ResultCode +" \ "); Sb. Append (","); Sb. Append ("\" errormsg\ ":" + "\" "+ errormsg +" \ "); Sb. Append (","); Sb. Append ("\" Datas\ ":" + "{"); Sb. Append ("\" filepath\ ":" + "\" "+ Paths +" \ "); Sb. Append (","); Sb. Append ("\" filename\ ":" + "\" "+ names +" \ "); Sb. Append (","); Sb. Append ("\" filetitle\ ":" + "\" "+ title +" \ "); Sb. Append (","); Sb. Append ("\" fileguid\ ":" + "\" "+ Fileguid +" \ "); Sb. Append ("}"); Sb. Append ("}"); } else {ResultCode = "1003"; errormsg = "Unsupported File types:" + strfileextension; Sb. Append ("{\" resultcode\ ":" + "\" "+ ResultCode +" \ "); Sb. Append (","); Sb. Append ("\" errormsg\ ":" + "\" "+ errormsg +" \ "); Sb. Append (","); Sb. Append ("\" Datas\ ":" + "{"); Sb. Append ("\" filepath\ ":" + "\" "+ Paths +" \ "); Sb. Append (","); Sb. Append ("\" filename\ ":" + "\" "+ names +" \ "); Sb. Append (","); Sb. Append ("\" filetitle\ ":" + "\" "+ Title + "\""); Sb. Append (","); Sb. Append ("\" fileguid\ ":" + "\" "+ Fileguid +" \ "); Sb. Append ("}"); Sb. Append ("}"); } context. Response.Write (sb.) ToString ()); } } }
ASP. NET generic handler receives file upload