Home>Article>Web Front-end> Ajax-based formData image and data upload

Ajax-based formData image and data upload

亚连
亚连 Original
2018-05-22 14:44:41 1538browse

This article mainly introduces the Ajax-based formData image and data upload related information in detail. It has certain reference value. Interested friends can refer to

I recently worked on a project about The project of user data and form upload has encountered many pitfalls. Here is a summary shared with everyone, hoping to help everyone. (Xiaobai, everyone is welcome to communicate more)

I won’t go into too much detail, just come to the code! !

1. Upload componentExplain that the project is based on the vue framework

Instructions, including two components for uploading images, the first one has multiple for multi-file mode , that is, multiple pictures can be selected at one time, followed by single file mode.

2. Next is the preview of the picture

viewimg($event) { //获取当前的input标签 var currentObj = event.currentTarget; //找到要预览的图片img标签,亦可动态生成 var img = currentObj.parentNode.children[0]; setImagePreview(currentObj, img); function setImagePreview(docObj, imgObjPreview) { if (docObj.files && docObj.files[0]) { imgObjPreview.style.display = 'block'; imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); } } }

The main function of this part is to display the selected picture. Of course, there are not multiple pictures here. Situation

3. Core part, image upload

/*采用formData形式上传图片和表单数据*/ upload: function() { var _self = this; var formData = new FormData(); var inputs = $("input.fileupload"); for (var i = 0; i < inputs.length; i++) { var file = inputs[i]; if (inputs[i].files[0]) { formData.append("file", file.files[0], file.files[0].name); } } formData.append('barterCommodityname', _self.goodsname); formData.append('barterSellingprice', _self.price); formData.append('barterContactinformation', _self.phone); formData.append('barterCommodityquantity', _self.number); formData.append('barterCommodityaddress', _self.address); formData.append('barterDescriptioninform', _self.goodsinfo); formData.append('barterCategoryid', _self.goodstype); var _self = this; $.ajax({ type: 'POST', url: 'http://10.145.0.05/goods/addGoods', dataType: "json", data: formData, processData: false, contentType: false, success: function(data) { console.log(data); if (data.code == 200) { console.log("success"); // _self.$router.push('/'); } else { alert(data.message); } } }); }

Description:

Similar to formData.append('barterCategoryid', _self.goodstype) ; is a form of key-value pairs to save data, and formData.append(“file”, file.files[0], file.files[0].name); The first parameter is the parameter name received by the server, The second parameter is the file object, and the third parameter is the file name, so that multiple files can be added to the server in the form of an array.

When the backend receives this type of file, the type is specified as: MultipartFile type

Special instructions:

processData: false,
contentType: false,

These two sentences must be added, otherwise the data will be serialized and the backend cannot recognize it

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

Solve the problem of ajax not supporting forward/back/refresh through history (graphic tutorial)

Ajax implements website hijacking detection method

Native JS implements Ajax cross-domain request flask response content (graphic tutorial)

The above is the detailed content of Ajax-based formData image and data upload. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn