File Upload is actually a basic page operation, which has been done before. In JSP + servlet, the key point is to specify that the enctype attribute of the form used by JSP to submit the file resource to be uploaded is multipart/form-data. This attribute sets the submitted content to be submitted to the servlet in the form of data streams. Of course, it also contains the file stream. In this way, you can use the interface method in common-fileupload. jar in the servlet to parse the stream and obtain the information of the uploaded file. </P> <p> today, when ext JS is used to replace JSP, it has encountered some minor problems. The following is a summary. </P> <p> In fact, the principle of using ext is the same, but it is important to write JavaScript code. When sending Ajax requests, make sure that the request is sent in the form where enctype is multipart/form-Data. Otherwise, the file information cannot be parsed in the background. </P> <p> using the following method can be used to define a form: </P> <p> var uploadform = new Ext. form. formpanel ({URL: "authmanager. do? Action = addcertificate ", // address of the Ajax request rame: True, // you must set this parameter to true fileupload: True, // you must specify this parameter, indicates that this form is used for file upload ID: 'uploadform', height: 130, labelalign: 'right', bodystyle: 'padding: 6px 5px 0', // enctype: 'multipart/form-data', // In fact, this item is useless. The fileupload: true command indicates the items: New Ext. form. textfield ({xtype: 'textfield', // style: 'margin-top: 15px ', ID: 'upload _ filepath', name: 'file', allowblank: false, fieldlabel: 'select file', Inputtype: 'file' // file format}) <br/> you can call uploadform to complete the price increase function. getform (). submit (); <br/> it should be noted that there is a way to cause upload failure: the form is still written as above, but there is no URL attribute in it, form is called when an Ajax request is sent: <br/> var uploadwin = new Ext. window ({Title: '<span style = "/" mce_style = "/" "font-size: 12px/"> Upload File </span>', width: 400, height: 130, items: [uploadform], // introduces the Upload File Form buttonalign: 'center', buttons: [{text: '<span style = "/" mce_style = "/" "font- Size: 12px/"> upload </span> ', Handler: uploadfile // call the function to complete the submission action}]}); // import the certificate function uploadfile () {</P> <p> Ext. ajax. request ({URL: "authmanager. do? Action = addcertificate ", </P> <p>...}) </P> <p >}< br/>
The upload request is the same as the first one, but the upload fails. From the thrown exception: "request must at least contains multipart/form-data or multipart /... stream, ...... "indicates that the request form is a normal default enctype form. What's going on? Although the previously uploaded form is correct, the request is not carried out in the correct form, which leads to an error. With the resources on the page, the background implementation is actually the same as JSP + servlet. The <SPAN class = 'wp _ keywordlink '> Code </span> is as follows: public String [] uploadfiletemp (httpservletrequest request) throws X {string [] filepaths = NULL; try {requestcontext CTX = new servletrequestcontext (request); Boolean ismultipart = fileupload. ismultipartcontent (CTX); // The form mode must be multi. out. println ("content type:" + CTX. getcontenttype (); If (ismultipart) {diskfileitemfact Ory factory = new diskfileitemfactory (); factory. setsizethreshold (1000*1024); // set the size saved to the memory: 1 m servletfileupload upload = new servletfileupload (factory); upload. setsizemax (500*1024*1024); // set the maximum size of the uploaded file to 500 mb list items = upload. parserequest (CTX); // parse the uploaded file unit in the request if (items! = NULL & items. size ()> 0) {filepaths = new string [items. size ()]; // initialize the returned file name array iterator it = items. iterator (); int flag =-1; while (it. hasnext () {flag ++; fileitem item = (fileitem) it. next (); Boolean isform = item. isformfield (); // whether it is a form field if (! Isform) {// if the form field is not suitable, It is the file upload string filename = item. getname (); // obtain the uploaded file name if (! Filename. equals ("") {// when this is reached, all the conditions required for file upload are met and can be uploaded, first, you must name the uploaded file string writefile = "" + system. currenttimemillis (); // name starting from time. Filepaths [flag] = writefile; system. out. println (flag + ":" + filepaths [flag]); writefile = constant. uploadpath + writefile; // file full path file = new file (writefile); item. write (File); // Upload File }}}} catch (exception e) {log. error ("failed to upload file:" + E. getmessage (); throw new x (). set (E);} return filepaths ;}