jq formdata upload pictures

html:<input type="file" name="" id="files" value="" class="files" />

js:$("#files").change(function (e) {
                 var formData = new FormData();
                var fileObj = $(this)[0];
                for(var i=0;i<fileObj.files.length;i++){
                    formData.append("file",fileObj.files[i]);
                }
                 formData.append("userID",longsession.userId);
                   $.ajax({
                       url:"url",
                    type: 'post',
                    cache: false,
                    data:formData,
                    processData: false,
                    contentType: false,
                      dataType: "json",
                    success : function(data) {
                        IF (data.result == "Success") {
                            mui.toast ( "photos uploaded successfully")
                        } the else {
                            mui.toast ( "Photo upload failed")
                        }
                        
                    },
                    error: function (xhr, of the type, errorThrown) {
                        MUI .toast ( "photo upload failed")
                        
                    }
                });
            });

Note: 1. want to check on the label plus multiple input multiple attributes,

2. Set the background (java is the case, other languages ​​do not know) or else cross-domain has been suggested:

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST,GET");
3. The transmission parameter may be set formData.append ( "name key", "key");

Guess you like

Origin www.cnblogs.com/zyb-722/p/11301275.html