JSP uses ajaxFileUpload. js to implement cross-origin issues, and ajaxfileupload cross-Origin

Source: Internet
Author: User

JSP uses ajaxFileUpload. js to implement cross-origin issues, and ajaxfileupload cross-Origin

If you don't talk much about it, You can directly post code to everyone.

The jsp code is as follows:

$. AjaxFileUpload ({url: 'http: // lh.abc.com: 8080/gap/fileUpload. do ', // the server-side request address used for file Upload (fxb.abc.com) secureuri: false, // It is generally set to false fileElementId: 'file ', // <input type = "file" id = "file" name = "file"/> dataType: 'jsonp ', // The return value type is generally set to json jsonp: 'jsoncallback', jsonpCallback: 'success _ jsonpcallback', function success_jsonpCallback (data) {alert ("1");}, success: function (data, status )// The server successfully responds to the processing function {alert (data. message); // extract the data in the message from the json returned by the server. The message is the member variable if (typeof (data. error )! = 'Undefined') {if (data. error! = '') {Alert (data. error);} else {alert (data. message) ;}}, error: function (data, status, e) // server response failure handler {alert (status); alert (e );}})

Configuration File

<action name="fileUpload" class="com.gap.action.FileUploadAction" method="fileUpload"><result type="json" name="success"><param name="contentType">text/html</param></result><result type="json" name="error"><param name="contentType">text/html</param></result></action>

Actions are processed as follows:

Public String fileUpload () throws Exception {String path = ServletActionContext. getRequest (). getRealPath ("/upload1"); // String path = ConfigDataInfo. getConfigValue ("imgServer"); try {File f = this. getFile (); if (this. getFileFileName (). endsWith (". exe ") {message =" sorry, the file format you uploaded is not allowed !!! ";}Else {FileInputStream inputStream = new FileInputStream (f); FileOutputStream outputStream = new FileOutputStream (path +"/"+ this. getFileFileName (); byte [] buf = new byte [1024]; int length = 0; while (length = inputStream. read (buf ))! =-1) {outputStream. write (buf, 0, length);} inputStream. close (); outputStream. flush (); message = "uploaded successfully" ;}} catch (Exception e) {e. printStackTrace (); message = "sorry, File Upload Failed !!!! ";}Return SUCCESS ;}

Each time an image is uploaded across domains, it can be successfully uploaded to the server, but the returned information cannot be correct. It is always in the error method, and the success method should be entered correctly.

Articles you may be interested in:
  • AjaxControlToolkit AjaxFileUpload
  • Jquery-ajaxfileupload asynchronous upload plug-in (with Engineering Code)
  • Use the ajaxfileupload plug-in to implement non-refreshing File Upload Methods
  • Implementation of jsp to prevent cross-Origin data submission
  • How to add additional parameters to jquery's ajaxfileupload
  • PHP + ajaxfileupload + jcrop plug-in perfectly achieves Avatar upload and tailoring
  • An example of a simple jQuery plug-in ajaxfileupload. js to implement ajax File Upload
  • Use ajaxfileupload. js to upload files to php through ajax

Related Article

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.