In order to record various problems encountered in the work and sort out the learning materials, we started to sort out previous articles and carefully record various problems in the learning process in the future.
The file upload function in HTML has always been a problem. In order to limit the size of the uploaded file, how to display the progress bar, and how to preview the image before uploading, you have also tried various methods, until one day you can see the image upload function in the Baidu editor. It took some time to extract it.
The final result is as follows:
This feature allows you to select and preview multiple image files, then delete the uploaded images in the upload queue, rotate and display the progress bar in the upload, and select an upload album.
Source code download path: http://pan.baidu.com/s/13iNYw
Structure
1: Flash file: imageuploader.swf
It is used to display the upload queue and provides the function of adding, previewing, deleting, rotating, and displaying progress bars to images!
Includes some parameter settings, such as the upload handler and related property settings. The most important parameter is flashvars, whose value is URL encoded.
The URL encoding conversion feature that you can implement through the http://tool.chinaz.com/Tools/URLEncode.aspx tool.
Original Value:
Container = flashcontainer & url =/baiduue/imageup & ext = {"param1": "value1", "param2": "value2"} & filetype = {"Description ": "image", "extension ":"*. GIF ;*. JPEG ;*. PNG ;*. jpg "watermark & width = 608 & Height = 272 & gridwidth = 121 & gridheight = 120 & picwidth = 100 & picheight = 100 & uploaddatafieldname = upfile & picdescfieldname = pictitle & maxsize = 4 & compresssize = 2 & maxnum = 32 & compressside = 0 & compresslength = 900
Remarks
- Container: "flashcontainer", // flash container ID
- URL: "/baiduue/imageup", // URL of the upload processing page
- Ext: '{"param1": "value1", "param2": "value2"}', // list of custom parameters that can be submitted to the server
- Filetype: '{"Description": "image", "extension ":"*. GIF ;*. JPEG ;*. PNG ;*. jpg "} ', // File Upload format restrictions
- Flashurl: 'imageuploader.swf ', // address of the flash component used for uploading
- Width: 608, // The Flash width
- Height: 272, // The height of Flash
- Gridwidth: 121, // The width of each preview Image
- Gridheight: 120, // The height of each preview Image
- Picwidth: 100, // The width of a preview Image
- Picheight: 100, // the height of a preview Image
- Uploaddatafieldname: "upfile", // key of the image data in the POST request
- Picdescfieldname: 'pictitle', // key of the image description in the POST request
- Maxsize: 4, // maximum file size, in MB
- Compresssize: 2, // if the image size exceeds this value before uploading, it will be compressed first, in MB
- Maxnum: 32, // The maximum number of files that can be uploaded at a time
- Compressside: 0, // benchmark for proportional compression. 0 indicates the longest side, 1 indicates the width, and 2 indicates the height.
- Compresslength: 900 // the maximum acceptable side length. If this value is exceeded, flash will automatically perform proportional compression.
2: JS script
Used to process external album selection, flash event, and Flash upload to upload images
************** * ************** // **** Check the flash status * @ private * @ Param {object} target Flash Object * @ return {Boolean} */function _ checkready (target) {If (typeof target! = 'Undefined' & typeof target. flashinit! = 'Undefined' & target. flashinit () {return true;} else {return false ;}} /*** create a random string * @ private * @ return {string} */function _ createstring () {var prefix = 'MW _ flash __'; return prefix + math. floor (math. random () * 2147483648 ). tostring (36 );} /*** create a function name * @ private * @ Param {string | function} for the incoming anonymous function */function name * @ return {string} */Function _ createfunname (fun) {var name = ''; name = _ createstr Ing (); window [name] = function () {fun. apply (window, arguments) ;}; return name ;}/ *** it is determined repeatedly that flash is completed by adding a callback function for flash .. */var interval = setinterval (function () {try {var flash = thismovie ("Flash"); If (_ checkready (flash )) {// you can use VaR callback = [] to poll a method of Flash; callback [0] = _ createfunname (selectfilecallback); callback [1] = _ createfunname (exceedfilecallback ); callback [2] = _ createfunname (deletefil Ecallback); callback [3] = _ createfunname (startuploadcallback); callback [4] = _ createfunname (uploadcompletecallback); callback [5] = _ createfunname (uploaderrorcallback ); callback [6] = _ createfunname (allcompletecallback); callback [7] = _ createfunname (changeheightcallback); thismovie ("Flash "). call ('setjsfuncname', [callback]); clearinterval (interval) ;}} catch (Ex) {}, 20); // obtain the function thism of the Flash Object Ovie (moviename) {If (navigator. appname. indexof ("misrosoft ")! =-1) {return window [moviename];} else {return document [moviename];}
3: General handler: Upload. ashx
Used to save images on ASP. NET image servers
String state = "success"; string url = NULL; string currenttype = NULL; string uploadpath = NULL; string filename = NULL; string originalname = NULL; httppostedfile uploadfile = NULL; public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; context. response. write (uploadpoto (context);} # region photo upload Public String uploadpoto (httpcontext context) {int aid = con Vert. toint32 (context. request. form ["aid"]); // obtain the album ID bool mark = convert. tostring (context. request. form ["mark"]). tolower () = "true"; // obtain the watermark. // upload the string pathbase = "upload/" + aid. tostring (); // storage path int size = 2; // file size limit, in MB string [] filetype = {". GIF ",". PNG ",". jpg ",". JPEG ",". BMP "}; // file format allowed // upload image hashtable info = new hashtable (); Info = upfile (context, pathbase, filetype, size, mark ); // Obtain the upload status String title = getotherinfo (context, "pictitle"); // obtain the image description string oriname = getotherinfo (context, "FILENAME "); // obtain the original file name string ret = "{'url': '" + info ["url"] + "', 'title': '" + title + "', 'original': '"+ oriname +"', 'state': '"+ info [" state "] +"'} "; return ret ;} private hashtable upfile (httpcontext context, string pathbase, string [] filetype, int size, bool mark) {pathbase = pathbase +" /"; Uploadpath = context. server. mappath (pathbase); // obtain the File Upload path try {uploadfile = context. request. files [0]; originalname = uploadfile. filename; // create createfolder () in the directory; // format verification if (checktype (filetype) {state = "unsupported file type ";} // size verification if (checksize (size) {state = "the file size exceeds the website limit";} // Save the image if (State = "success ") {filename = Rename (); string smallpath = resmallname (); string waterpath = repicname (); uploa Dfile. saveas (uploadpath + filename); string savepath = uploadpath + filename; imageclass = new imageclass (); If (imageclass. showthumbnail (savepath, uploadpath + smallpath, 640) // if there is a thumbnail, delete the source image {fileinfo Fi = new fileinfo (savepath); FI. delete (); url = pathbase + smallpath; filename = smallpath;} else {url = pathbase + filename;} If (Mark) {string watxt = "I Am a watermark... "; If (imageclass. writertext (uploadpath + filename, uploadpath + waterpath, watxt) {url = pathbase + waterpath; filename = waterpath ;}}} catch (exception E) {state = "Unknown error"; url = "" ;}return getuploadinfo ();} # endregion # auxiliary method of region uploading files/*** obtain file information * @ Param context * @ Param string * @ return string */protected string getotherinfo (httpcontext context, string field) {string info = NULL; If (context. Request. Form [field]! = NULL &&! String. isnullorempty (context. Request. Form [field]) {info = field = "FILENAME "? Context. request. form [field]. split (',') [1]: context. request. form [field];} return Info;}/*** get upload information * @ return hashtable */protected hashtable getuploadinfo () {hashtable infolist = new hashtable (); infolist. add ("State", State); infolist. add ("url", URL); If (currenttype! = NULL) infolist. Add ("currenttype", currenttype); If (originalname! = NULL) infolist. add ("originalname", originalname); Return infolist;}/*** rename the file * @ return string */protected string Rename () {return system. guid. newguid () + getfileext ();} protected string resmallname () {return system. guid. newguid () + "_ small" + getfileext ();} protected string repicname () {return system. guid. newguid () + "_ POTO" + getfileext ();}/*** file type check * @ return bool */protected boo L checktype (string [] filetype) {currenttype = getfileext (); Return array. indexof (filetype, currenttype) =-1;}/*** file size check * @ Param int * @ return bool */protected bool checksize (INT size) {return uploadfile. contentlength> = (size * 1024*1024);}/*** get file extension * @ return string */protected string getfileext () {string [] temp = uploadfile. filename. split ('. '); return ". "+ temp [temp. length- 1]. tolower ();}/*** automatically create a storage folder by date */protected void createfolder () {If (! Directory. exists (uploadpath) {directory. createdirectory (uploadpath) ;}# endregion
For more details, refer to the source code. For some children's shoes, Chrome is not supported. I think it should be the problem of Flash HTML encoding. Recently I am busy, so it will not take time to deal with it.