WEBAPI implementation of mobile upload Avatar interface

Source: Internet
Author: User
Tags save file

You can send requests directly using Postman impersonation when testing

        /// <summary>        ///phone-side upload Avatar interface/// </summary>        /// <param name= "Logintoken" ></param>        /// <param name= "base64string" ></param>        /// <returns></returns>[HttpPost] Public AsyncTaskUploadavatar () {//Check if it is Multipart/form-data            if(! Request.Content.IsMimeMultipartContent ("Form-data"))                returnCreateresponse (false,"Please select a file to upload! "); //Set upload directory            stringRoot = System.Web.HttpContext.Current.Server.MapPath ("/upload/avatars/"); varProvider =NewRenamemultipartformdatastreamprovider (root); stringLogintoken = system.web.httpcontext.current.request.params["Logintoken"]; vartoken =NewGuid (Logintoken); varLogininfo = This.            Mobileservice.getlogininfo (token); if(Logininfo = =NULL)            {                returnCreateresponse (false,"Login Authorization failed! "); }            awaitRequest.Content.ReadAsMultipartAsync (provider); varFile = provider. filedata[0]; varFileInfo =NewFileInfo (file.            LocalFilename); varFileStream =Fileinfo.openread (); varIMG =System.Drawing.Image.FromStream (FileStream); Uploadavatar Upload=NewUploadavatar (); varresult =upload.            Mobileuploadimage (IMG); //Delete intermediate file//remove occupy Todo//if (file.exists (File. LocalFilename))//{            //File.delete (File.            LocalFilename); //}            if(result.success) {varperson = This.                Foundationservice.getpersoninfobyid (Logininfo.userid); if(Person! =NULL) {person. Photo= Result.sourceurl;//Avatar thumbnail Address                     This.                    Foundationservice.updatepersoninfo (person); Result.sourceurl="http://192.168.3.66:8008"+Result.sourceurl; }            }            returnCreateresponse (true, result); }
 PublicResult mobileuploadimage (Image _file) {BOOLIsthumbnail =true; Try            {                stringFileext ="jpg";//file extension, not including "."Path= HttpContext.Current.Server.MapPath ("/upload/avatars/");//Upload avatar Path                stringNewFileName = Getramcode () +"."+ Fileext;//randomly generate a new file name                stringNewthumbnailfilename ="thumb_"+ NewFileName;//randomly generated thumbnail file names                stringUploadpath ="/upload/avatars/yt/"+ NewFileName;//Upload Original directory relative path                stringUploadpaths ="/upload/avatars/slt/"+ NewFileName;//upload thumbnail directory relative path//whether there is a folder where thumbnails and originals are stored is not created                stringPathS = HttpContext.Current.Server.MapPath ("/upload/avatars/slt/"); stringPathy = HttpContext.Current.Server.MapPath ("/upload/avatars/yt/"); if(!directory.exists (PathS))                {directory.createdirectory (PathS); }                if(!directory.exists (pathy))                {directory.createdirectory (pathy); }                stringNewfilepath = path +"yt\\"+ NewFileName;//the path of the original image after uploading                stringNewthumbnailpath = path +"slt\\"+ NewFileName;//thumbnail path after uploading                #regionCheck if the physical path of the upload exists and does not exist createif(!directory.exists (path))                {directory.createdirectory (path); }                #endregion                #regionSave File_file.                Save (Newfilepath); #endregion                #regionPicture clipping//if it is a picture, check whether you need to generate a thumbnail, or generate                if(Isimage (fileext) && isthumbnail && thumbnailwidth >0&& thumbnailheight >0) {thumbnail.makethumbnailimage (Newfilepath, Newthumbnailpath, Thumbnailwidth, Thum Bnailheight,"Cut"); }                Else{Newthumbnailpath= Newfilepath;//returns the original image without generating thumbnails                }                #endregion                //finished processing, return file information in JOSN format                return NewResult () {success =true, msg ="Operation Successful! ", sourceURL =uploadpaths}; }            Catch            {                return NewResult () {success =false, msg ="An unexpected error occurred during the upload process! " }; }        }

What to note when using the Postman simulation test:

Body in Request type select Form-data Select File Parameter key is Multipartfile

Do not check the headers in the

WEBAPI implementation of mobile upload Avatar interface

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.