ASP. NET generic handler receives file upload

Source: Internet
Author: User
Tags save file

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.