PublicActionResult Upload () {HttpPostedFileBase file= request.files["Fdata"];//Accept File Data stringFileName = path.getfilename (file. FileName);//Get file name stringFileext = Path.getextension (FileName);//Get extension if(Fileext = =". jpg") { //file. SaveAs (Request.mappath ("/uploadfile/" + FileName); stringDIR ="/uploadfile/"+ DateTime.Now.Year +"/"+ DateTime.Now.Month +"/"+ DateTime.Now.Day +"/"; Directory.CreateDirectory (Request.mappath (dir));//Create a folder stringFulldir = dir + GetStreamMD5 (file. InputStream) + Fileext;//build the full pathFile. SaveAs (Request.mappath (Fulldir));//Save the full picture stringThumbnalpath = dir + guid.newguid () +". jpg"; Makethumbnail (Request.mappath (Fulldir), Request.mappath (Thumbnalpath), -, -,"H");//Save thumbnail image using(Image img =Image.FromFile (Request.mappath (Thumbnalpath))) { returnJson (New{ImagePath=Thumbnalpath, Width=img. Width, Height=img. Height}); } } Else { returnContent ("failed to upload file"); } } /// <summary> ///calculate the MD5 value of a file/// </summary> /// <param name= "filepath" ></param> /// <returns></returns> Public StaticString GetStreamMD5 (Stream stream) {stringStrresult =""; stringStrhashdata =""; byte[] arrbythashvalue; System.Security.Cryptography.MD5CryptoServiceProvider Omd5hasher=NewSystem.Security.Cryptography.MD5CryptoServiceProvider (); Arrbythashvalue= Omd5hasher.computehash (stream);//computes the hash value of the specified Stream object//A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "f-2c-4a"Strhashdata =System.BitConverter.ToString (Arrbythashvalue); //Replace-Strhashdata = Strhashdata.replace ("-",""); Strresult=Strhashdata; returnstrresult; } #regionThumbnail images/// <summary> ///generate thumbnail images/// </summary> /// <param name= "Originalimagepath" >Source Map Path (physical path)</param> /// <param name= "Thumbnailpath" >thumbnail path (physical path)</param> /// <param name= "width" >thumbnail width</param> /// <param name= "height" >thumbnail height</param> /// <param name= "mode" >how thumbnails are generated</param> Public Static voidMakethumbnail (stringOriginalimagepath,stringThumbnailpath,intWidthintHeightstringmode) {System.Drawing.Image Originalimage=System.Drawing.Image.FromFile (Originalimagepath); intTowidth =width; intToheight =height; intx =0; inty =0; intow =Originalimage.width; intOh =Originalimage.height; Switch(mode) { Case "HW"://Specify aspect scaling (possibly deformed) Break; Case "W"://specify wide, high proportionallyToheight = originalimage.height * Width/Originalimage.width; Break; Case "H"://specify high, wide proportionallyTowidth = originalimage.width * Height/Originalimage.height; Break; Case "Cut"://designation Aspect cut (not deformed) if((Double) Originalimage.width/(Double) Originalimage.height > (Double) Towidth/(Double) toheight) {Oh=Originalimage.height; ow= Originalimage.height * Towidth/Toheight; Y=0; X= (Originalimage.width-ow)/2; } Else{ow=Originalimage.width; Oh= originalimage.width * Height/Towidth; X=0; Y= (Originalimage.height-oh)/2; } Break; default: Break; } //Create a new BMP imageSystem.Drawing.Image bitmap =NewSystem.Drawing.Bitmap (Towidth, toheight); //Create a new artboardSystem.Drawing.Graphics g =System.Drawing.Graphics.FromImage (bitmap); //setting high-quality interpolation methodsG.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.High; //set high quality, low speed rendering smoothnessG.smoothingmode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; //Empty the canvas and fill it with a transparent background colorg.clear (System.Drawing.Color.Transparent); //draws the specified portion of the original picture at the specified position and at the specified sizeG.drawimage (Originalimage,NewSystem.Drawing.Rectangle (0,0, Towidth, Toheight),NewSystem.Drawing.Rectangle (x, Y, Ow, OH), System.Drawing.GraphicsUnit.Pixel); Try { //save thumbnails in jpg formatbitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg); } Catch(System.Exception e) {Throwe; } finally{originalimage.dispose (); Bitmap. Dispose (); G.dispose (); } } #endregion
Image upload processing server side