簡體   English   中英

使用laravel在angularjs和后端上傳多個文件(不帶表單標簽)

[英]Upload multiple files in angularjs and backend using laravel (without form tag)

我想在不使用表單標簽和使用 angular js 的情況下上傳多個文件,我使用的是 laravel 5.2。 下面的代碼我嘗試只發送 1 個文件,但它在多個文件上失敗。

HTML代碼

<input type="file" class="upload_file pull-right" multiple />

查詢代碼

$(".upload_file").change(function ()
{
    if (this.files && this.files.length > 0)
    {
         scope.uploadFile(this.files);
    }
});

AngularJs 代碼

scope = $scope;

$scope.uploadFile = function(files)
{
    var fd = new FormData();
    fd.append("file", files);
    $http.post(API_URL + '/offline_upload/file/?token=' + token,fd,
    {
        withCredentials: true,
        headers: {'Content-Type': undefined },
        transformRequest: angular.identity
    })
    .success(function(data)
    {
        alert('Done');
    });
 };

Laravel PHP 代碼

$files = $request->file('file');
Log::info(count($files));

這里的計數始終為 0

在 angularjs 而不是文件中,如果我發送文件 [0],則只發送 1 個文件。 但是如何發送多個文件並檢索它。

有人可以幫我解決這個問題。

您可以嘗試使用https://github.com/nervgh/angular-file-upload ,它支持具有多個參數的輸入類型文件,並且您不需要 jquery。

您只需設置新的上傳器對象: $scope.uploader = new FileUploader({var options;}); 並將指令放入 html 中的上傳表單<input type="file" nv-file-select uploader="uploader"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM