如果您对通过JQueryAJAX一起发送FormData和String数据吗?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于通过JQueryAJAX一起发送FormData
如果您对通过JQuery AJAX一起发送FormData和String数据吗?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于通过JQuery AJAX一起发送FormData和String数据吗?的详细内容,并且为您提供关于Ajax发送formdata数据,SpringMVC后台处理、c# – 如何通过jquery ajax发送AntiForgeryToken(CSRF)和FormData、javascript – jQuery / AJAX – 将附加数据与文件上传一起发送、javascript – JS:如何使用FormData发送多个文件(jQuery Ajax)的有价值信息。
本文目录一览:- 通过JQuery AJAX一起发送FormData和String数据吗?
- Ajax发送formdata数据,SpringMVC后台处理
- c# – 如何通过jquery ajax发送AntiForgeryToken(CSRF)和FormData
- javascript – jQuery / AJAX – 将附加数据与文件上传一起发送
- javascript – JS:如何使用FormData发送多个文件(jQuery Ajax)
通过JQuery AJAX一起发送FormData和String数据吗?
我该如何发布文件并输入字符串数据FormData()
?例如,我还有许多其他 隐藏的输入数据 ,需要将它们发送到服务器,
html,
<form action="image.php" method="post" enctype="multipart/form-data"><input type="file" name="file[]" multiple="" /><input type="hidden" name="page_id" value="<?php echo $page_id;?>"/><input type="hidden" name="category_id" value="<?php echo $item_category->category_id;?>"/><input type="hidden" name="method" value="upload"/><input type="hidden" name="required[category_id]" value="Category ID"/></form>
使用下面的这段代码,我仅设法发送文件数据 ,而不 发送隐藏的输入数据。
jQuery,
// HTML5 form data object.var fd = new FormData();var file_data = object.get(0).files[i];var other_data = $(''form'').serialize(); // page_id=&category_id=15&method=upload&required%5Bcategory_id%5D=Category+IDfd.append("file", file_data);$.ajax({ url: ''add.php'', data: fd, contentType: false, processData: false, type: ''POST'', success: function(data){ alert(data); }});
server.php
print_r($_FILES);print_r($_POST);
结果,
Array( [file] => Array ( [name] => xxx.doc [type] => application/msword [tmp_name] => C:\wamp\tmp\php7C24.tmp [error] => 0 [size] => 11776 ))
我希望得到这个结果,
Array( [file] => Array ( [name] => xxx.doc [type] => application/msword [tmp_name] => C:\wamp\tmp\php7C24.tmp [error] => 0 [size] => 11776 ))Array( [page_id] => 1000 [category_id] => 12 [method] => upload ...)
可能吗?
答案1
小编典典var fd = new FormData();var file_data = $(''input[type="file"]'')[0].files; // for multiple filesfor(var i = 0;i<file_data.length;i++){ fd.append("file_"+i, file_data[i]);}var other_data = $(''form'').serializeArray();$.each(other_data,function(key,input){ fd.append(input.name,input.value);});$.ajax({ url: ''test.php'', data: fd, contentType: false, processData: false, type: ''POST'', success: function(data){ console.log(data); }});
添加了一个for
循环,并在中将更改.serialize()
为.serializeArray()
,以供对象引用.each()
附加到FormData
。
Ajax发送formdata数据,SpringMVC后台处理
撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>>
前端js:
var submit_data = new FormData();
var name = $("#name").val();
let file = $("#file")[0].file[0];
submit_data.append("file", file);
submit_data.append("name", name);
$.ajax({
url: "http://localhost:8080/caochanglu/getSearch/getRule",
type: "POST",
data: submit_data,
async: false,
dataType: "json",
cache: false,
processData: false,
contentType: false,
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
}
});
后台spring
@ResponseBody
@RequestMapping(value = "getRule", methon = {RequestMethod.GET, RequestMethod.POST})
public String getRule(HttpServletRequest request, @RequestParam("file") MultipartFile file){
String name = request.getParameter("name");
}
c# – 如何通过jquery ajax发送AntiForgeryToken(CSRF)和FormData
视图
@using (Html.BeginForm("Upload","RX",FormMethod.Post,new {id = "frmRXUpload",enctype = "multipart/form-data"})) { @Html.AntiForgeryToken() @Html.TextBoxFor(m => m.RXFile,new {.type = "file"}) ...rest of code here } <script> $(document).ready(function(){ $('#btnRXUpload').click(function () { var form = $('#frmRXUpload') if (form.valid()) { var formData = new FormData(form); formData.append('files',$('#frmRXUpload input[type="file"]')[0].files[0]); formData.append('__RequestVerificationToken',fnGetToken()); $.ajax({ type: 'POST',url: '/RX/Upload',data: formData,contentType: false,processData: false }) } }) }) </script>
调节器
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Upload() { //rest of code here }
我明白了
The anti-forgery token Could not be decrypted. If this application is hosted by a Web Farm or cluster
通过提琴手的错误.不知道如何解决这个问题?
我发现了答案:
<script> $(document).ready(function(){ $('#btnRXUpload').click(function () { var form = $('#frmRXUpload') if (form.valid()) { var formData = new FormData(form.get(0)); //add .get(0) formData.append('files',$('#frmRXUpload input[type="file"]')[0].files[0]); $.ajax({ type: 'POST',processData: false }) } }) }) </script>
解决方法
我只需要在表单中添加.get(0),这里是代码:
<script> $(document).ready(function(){ $('#btnRXUpload').click(function () { var form = $('#frmRXUpload') if (form.valid()) { var formData = new FormData(form.get(0)); //add .get(0) formData.append('files',$('#frmRXUpload input[type="file"]')[0].files[0]); //formData.append('__RequestVerificationToken',fnGetToken()); //remark this line $.ajax({ type: 'POST',processData: false }) } }) }) </script>
javascript – jQuery / AJAX – 将附加数据与文件上传一起发送
我使用jQuery将文件上传到服务器:
$.ajax({
url : 'http://www.example.com',
dataType : 'json',
cache : false,
contentType : false,
processData : false,
data : formData, // formData is $('#file').prop('files')[0];
type : 'post',
success : function(response) {something}
});
我想与文件一起发送其他参数.可能吗?如果是 – 如何?
谢谢!
解决方法:
要发送其他参数,您只需将其附加到formdata,如下所示:
var formdata=new FormData();
formdata.append('simpleFile', $('#file').get('files')[0]); //use get('files')[0]
formdata.append('someotherparams',someothervalues);//you can append it to formdata with a proper parameter name
$.ajax({
url : 'http://www.example.com',
dataType : 'json',
cache : false,
contentType : false,
processData : false,
data : formData, //formdata will contain all the other details with a name given to parameters
type : 'post',
success : function(response) {something}
});
javascript – JS:如何使用FormData发送多个文件(jQuery Ajax)
在我的表单中有多个文件上传,使用FormData只上传一个文件,虽然我选择了多个要上传的文件,以下是代码
HTML
<form name="uploadImages" method="post" enctype="multipart/form-data">
<input type="file" name="photo[]" value="">
<input type="file" name="photo[]" value="">
<input type="file" name="photo[]" value="">
</form>
JS
var ajaxData = new FormData();
ajaxData.append( 'action','uploadImages');
jQuery.each($("input[name^='photo']")[0].files, function(i, file) {
ajaxData.append('photo['+i+']', file);
});
$.ajax({
url: URL,
data: ajaxData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
dataType:'json',
success: function(data) {
if (data.status == 'success') {
location.reload();
}
}
});
我在服务器上使用PHP,使用HTML属性名称i,e照片我只能保存文件,动态文件名对我不起作用.
解决方法:
你在javascript中有一个错误:你只在一个输入中迭代文件,请看看这个
var ajaxData = new FormData();
ajaxData.append( 'action','uploadImages');
$.each($("input[type=file]"), function(i, obj) {
$.each(obj.files,function(j, file){
ajaxData.append('photo['+j+']', file);
})
});
例如jsfiddle
今天关于通过JQuery AJAX一起发送FormData和String数据吗?的分享就到这里,希望大家有所收获,若想了解更多关于Ajax发送formdata数据,SpringMVC后台处理、c# – 如何通过jquery ajax发送AntiForgeryToken(CSRF)和FormData、javascript – jQuery / AJAX – 将附加数据与文件上传一起发送、javascript – JS:如何使用FormData发送多个文件(jQuery Ajax)等相关知识,可以在本站进行查询。
本文标签: