2017-09-02 21:44
浏览 70

Laravel - 从Ajax Post请求中获取数据

I want to upload images with ajax. I convert the image to a base64 string. Posting the data works, but how do I get it on the server (I'm using Laravel 5.4)? I can do $request->all() what gives me an array with all the images base64 strings combined. I cannot do anything with that because whatever I do with that array will result in a 500 error.

This is my script to convert the images and post them.

let queue = [];
function addFile(input) {
    let files = input.files,
        j = files.length,

    for (i = 0; i < j; i += 1) {
        let reader = new FileReader();
        reader.onloadend = function (e) {
                "<div class='upload-ItemPanel'><img class='upload-ImagePreview' src='" + + "' > </div>");
        file = files[i];

$('#upload-ButtonSelect').on("click" , function () {

$('#upload-UploadInput').change(function () {

$('#upload-ButtonUpload').click(function () {

        url: "/admin/upload",
        type: "POST",
        data: queue,
        processData: false,
        error: function(xhr, status, error) {
            let err = xhr.responseText;
            $('#upload-InnerPanel').append("<iframe width='600' height='500' src='" + err +"'> </iframe>")
        success: function (xhr) {
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')


This is my controller:

public function upload(Request $request)

    return var_dump($request->all());

That works, sort of, because My response is one long base64 in an array with just 1 item. Even if I add multiple images I just get one item in the array instead of three. It now combines them all in to one. Also, as I said. I cannot do anything with that array what does not result in a 500 error.

So my question is:

How do I get it to work so I can post multiple items instead of one and get the data on the backend?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题