Filehelper for new users

Source: Internet
Author: User

only the private method in the filehelper class is provided here, and the public method is an overload of it

Private Static bool uploadimage (fileupload Fu, string fullsizedfolderpath, string thumbnailfolderpath, bool createthumbnail, int width, int height, string inname, out string outname) {page = httpcontext. current. handler as page; clientscriptmanager CSM = page. clientscript; outname = inname; If (Fu. hasfile) {If (Fu. postedfile. contenttype. tolower (). indexof ("image")> = 0) {If (string. isnullorem Pty (inname) {outname = guid. newguid (). tostring (). replace ("-", "");} else {outname = inname. split ('. ') [0];} string ext = Fu. filename. split ('. ') [1]; system. drawing. imaging. imageformat imgformat = getimageformat (EXT); outname = outname + ". "+ ext; string oripath = httpcontext. current. server. mappath ("~ /"+ Fullsizedfolderpath); string minpath = string. Empty; If (! Directory. exists (oripath) {directory. createdirectory (oripath);} Try {system. drawing. image IMG = system. drawing. image. fromstream (Fu. postedfile. inputstream); IMG. save (oripath + outname, imgformat); If (createthumbnail) {minpath = httpcontext. current. server. mappath ("~ /"+ Thumbnailfolderpath); If (! Directory. exists (minpath) {directory. createdirectory (minpath);} float heightmultipier = (float) height/(float) IMG. height; float widthmultipier = (float) width/(float) IMG. width; If (heightmultipier> 1) heightmultipier = 1; if (widthmultipier> 1) widthmultipier = 1; float sizemultiplier = widthmultipier 

The method is as follows:

For the add operation, the inname parameter can be string. empty, and the output parameter outname is the image file name, that is, the data to be stored in the database;

If it is a modification operation, the inname parameter is the original file name, and the output parameter is the same as above. In most cases, the two are the same, but when the suffix is different, the file cannot be overwritten! Therefore, there is a deletefile judgment operation, and the two are different.

Two auxiliary methods used in the method: getimageformat and deletefile (this is quite simple, not given)

///  /// obtain the image format based on the image file suffix ///  ///  NO. file suffix  /// 
  
    imageformat 
   Private Static system. drawing. imaging. imageformat getimageformat (string ext) {Switch (ext. toupper () {Case "GIF": return system. drawing. imaging. imageformat. GIF; Case "Jpeg": Case "jpg": return system. drawing. imaging. imageformat. JPEG; Case "BMP": Re Turn system. drawing. imaging. imageformat. BMP; Case "PNG": return system. drawing. imaging. imageformat. PNG; Case "ICO": return system. drawing. imaging. imageformat. icon; default: Throw new exception ("invalid image format! ") ;}}

at the beginning, this method can still handle the problem. Gradually, gradually! I gradually wrote another method:

/// <Summary> /// obtain the server address of the thumbnail of the image /// </Summary> /// <Param name = "filepath"> source image server address </param> /// <Param name = "maxwidth"> maximum width of a thumbnail </param> /// <Param name = "maxheight"> maximum height of a thumbnail </param> /// <returns> </returns> Public static string getthumbimage (string filepath, int maxwidth, int maxheight) {If (string. isnullorempty (filepath) {return string. empty;} int Pos = filepath. lastindexof ('/'); string vpath = filepa Th. remove (POS); fileinfo finfo = new fileinfo (httpcontext. current. server. mappath (filepath); string filename = finfo. name; string Foldername = finfo. directoryname; If (file. exists (string. format ("{0}/{1}-{2}/{3}", Foldername, maxwidth, maxheight, filename) {return string. format ("{0}/{1}-{2}/{3}", vpath, maxwidth, maxheight, filename);} else {try {string thumbfolder = string. format ("{0 }\\ {1 }-{ 2} ", Foldername, maxwidth, maxheight); system. Drawing. Image IMG = system. Drawing. image. fromfile (finfo. fullname); If (! Directory. exists (thumbfolder) {directory. createdirectory (thumbfolder);} float heightmultipier = (float) maxheight/(float) IMG. height; float widthmultipier = (float) maxwidth/(float) IMG. width; If (heightmultipier> 1) heightmultipier = 1; if (widthmultipier> 1) widthmultipier = 1; float sizemultiplier = widthmultipier 

Thumbnails are not used in one place. They are dizzy! This method is much better!

Execution idea: Request the thumbnail path based on the source image path filepath. For example, if the image/a.jpgwants maxwidthto be 150 and maxheightto be 120, the first execution will generate a thumbnail for a.jpg and save it as image/-/a.jpg,

And return "image/150-120/a.jpg". For future requests, the thumbnail path is directly returned. If you need a thumbnail of up to 100*80 in another place, it is okay to use getthumbimage ("image/a.jpg", 80!

 

Simple File Download:

Public static void downloadfile (string strdownfile) {string STR = httpcontext. current. server. mappath (strdownfile); If (file. exists (STR) {fileinfo Fi = new system. io. fileinfo (STR); httpcontext. current. response. clear (); httpcontext. current. response. clearheaders (); httpcontext. current. response. buffer = false; httpcontext. current. response. contenttype = "application/octet-stream"; httpcontext. current. Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (Fi. fullname, system. text. encoding. getencoding ("gb2312"); httpcontext. current. response. appendheader ("Content-Length", Fi. length. tostring (); httpcontext. current. response. writefile (Fi. fullname); httpcontext. current. response. flush (); httpcontext. current. response. end ();} else {page = httpcontext. curre NT. handler as page; page. clientscript. registerstartupscript (page. GetType (), "js22", "<SCRIPT> alert ('this file path does not exist! '); </SCRIPT> ");}}

If you make a slight modification, You can discard this method for general processing.ProgramTo download requests!

. I have a good rest tomorrow. I have to go to bed with RPG! Ha

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.