当前位置: 首页 > 知识库问答 >
问题:

如何通过ajax使用普通JavaScript将多张照片上传到Laravel应用程序?

冉永宁
2023-03-14
<form action="" method="POST" enctype="multipart/form-data">
            @csrf
                <label for="photos">Photos:</label>
                <input type="file" name="photos[]" id="photos" class="form-control" multiple>


                <button class="btn btn-success mt-3" onclick="ajaxify(event)">Submit</button>
            </div>
        </form>
function ajaxify(event) {
            event.preventDefault();
            let failedValidation = false;

           // I removed parts of the code where I load other dataand do validation, irrelevant to the question.

            let photos = [];


            if(document.getElementById('photos').value !== '') {
                photos = document.getElementById('photos');   // I know this is incorrect, but I don't know what to do here.
            }

           // Here photos.value return something like c://fake/filename
           // And it doesn't return more than 1 file even, so anyway I am definitely doing this wrong.


            if(! failedValidation) {
                axios.post('/listing/create', {
                    client_name: name.value,
                    client_phone_number: client_phone_number.value,
                    category: category.value,
                    type: type.value,
                    governorate: governorate.value,
                    city: city.value,
                    space: space.value,
                    price: price.value,
                    furnished_status: furnished_status.value,
                    payment_type: payment_type.value,
                    initial_deposit: initial_deposit.value,
                    monthly_amount: monthly_amount.value,
                    notes: notes.value,
                    photos: photos.value, // So this should be an array of uploaded files.
                })
                .then((resp) => {
                    invalid.classList.add('d-none');
                    console.log(resp);
                })
            }
        }

一个简短的说明,我正在使用Laravel媒体库包,如果这有什么不同的话。

到目前为止,我所做的是研究这个问题,我读到我需要使用FormData(),我以前从未使用过它,我有几个问题,我需要将所有数据放在FormData()对象中并将其提供给Axios吗?还是我只需要它拍照片?我还没有尝试做这两件事中的任何一件,任何指导都将非常感谢。

共有1个答案

吕寒
2023-03-14

您只能获得一个文件,因为所有文件对象都存储在files属性中的数组中。只需将它们追加到photos数组。

function ajaxify(event) {
  event.preventDefault();

  // use files attribute to get an array of the files
  var photoFiles = document.getElementById("photos").files;

  // using the array of files, create an array 'photos' of FormData objects
  let photos = [];
  for (let photo of photoFiles) {
    photos.push(new FormData(photo);
  }

  // turn your 'photos' array into a javascript object
  let photos = arr2obj(photoFiles);

  // this should fix the empty array problem you were having
  // pass 'photos' to the ajax data

  // ....
}

编辑:根据这篇文章,使用AJAX上传文件需要一个FormData对象,正如其中一位评论者指出的那样。数组必须是FormData对象数组。

编辑:通过JSON发送数组很麻烦。将数组转换为对象。您可以使用类似这样的简单函数从数组中构建一个对象。

function arr2obj(arr) {
  var obj = {};
  for (let i=0; i<arr.length; i++) {
    obj['photo'+i] = arr[i];
  }
  return obj;
}
 类似资料:
  • Instagram最近改变了他们的API政策,允许开发者通过自己的应用程序将图片发布到Instagram平台。我们以前使用的几种其他技术来实现这一目标。其中之一是调用Instagram应用程序,该应用程序基本上可以打开Instagram并在那里进行共享。关于如何做到这一点的教程可以在这里看到:如何从你自己的iOS应用程序分享图像到Instagram 然而,有几个应用程序允许直接共享到Instagr

  • 问题内容: 我尝试了不同的方法来通过imgur上传和检索链接,但是尽管查看了imgur api,但都没有成功。 http://api.imgur.com/examples#uploading_java 但是以下方法部分有效。.我试图检索, 错误:如果发生任何错误。指向图像的链接:指向托管图像的链接删除链接:删除到托管图像的链接 但是我只得到“删除链接”,因为其他的都是空白,请检查一下: 我最后得到

  • 为了便于学习,我正在使用oop php开发一个用于属性/广告的cms。我试图上传多张照片,这些照片通过具有特定属性的透视表连接,但插入这些照片时遇到问题。当我插入包含两张或多张照片的属性时,我需要这些照片在透视表中具有不同的id,但属性的id相同。我一次成功拍摄了一张照片,但多次出现错误: 警告:explode()期望参数2是字符串,当I var dump$tmp变量I为null且 警告:end(

  • 我得到了这个endpoint,但我不知道如何从电话画廊拍照并通过这个endpoint发送此图像?

  • 问题内容: 我是在项目中使用代码点火器的新用户,上载多个文件时遇到一个问题,但最后一个仅插入到所有图像“三个图像”字段中。 我的控制器是: 我的观点是: 请帮助如何插入3张图像。 我的资料库 问题答案: HTML: PHP的:

  • 本文向大家介绍如何通过AJAX调用传递JavaScript变量?,包括了如何通过AJAX调用传递JavaScript变量?的使用技巧和注意事项,需要的朋友参考一下 要通过AJAX调用传递JavaScript变量,请替换以下内容- 与- 示例 尝试以下代码以通过AJAX调用正确传递变量-