gpt4 book ai didi

php - laravel中的AJAX文件上传

转载 作者:可可西里 更新时间:2023-11-01 00:09:11 25 4
gpt4 key购买 nike

自从过去两天以来,我一直在尝试让 ajax 文件上传在 lavavel 4 中工作,但我现在很不走运。

我的 jquery block

$(document).ready(function(){

$('#basicModuleImage').change(function () {
sendFile(this.files[0]);
});

function sendFile(file) {

$.ajax({
type: 'post',
url: '/upload',
data: file,
enctype: 'multipart/form-data',
success: function (data) {
alert(data);
},
processData: false,
contentType: file.type
});
}
});

HTML block

 <form method="post" action="">
<input type="file" id="basicModuleImage" name="basicModuleImage" />
</form>

LARAVEL PHP block

Route::post('upload', function(){

return Response::json(array('a'=>var_dump(Input::all()),'b'=>var_dump($_FILES)));

});

我也试过使用 https://github.com/LPology/Simple-Ajax-Uploader但它似乎是 laravel 的问题。

JSON 响应返回 a 和 b 均为 null。

最佳答案

关键在您的 ajax 请求中。在 Controller 中你可以做任何你想做的事情。

var form = document.forms.namedItem("yourformname"); // high importance!, here you need change "yourformname" with the name of your form
var formdata = new FormData(form); // high importance!

$.ajax({
async: true,
type: "POST",
dataType: "json", // or html if you want...
contentType: false, // high importance!
url: '{{ action('yourController@postMethod') }}', // you need change it.
data: formdata, // high importance!
processData: false, // high importance!
success: function (data) {

//do thing with data....

},
timeout: 10000
});

关于php - laravel中的AJAX文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22399322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com