簡體   English   中英

Laravel和Dropzone-如何在服務器端獲取文件

[英]Laravel and Dropzone - How to get files in server side

我試圖在laravel項目中使用dropzone,但無法在服務器端獲取文件。

我的刀片html代碼:

{!! Form::open(['id' => 'create-intervention-form', 'url' => '/create-intervention', 'method' => 'post', 'class' => '']) !!}
   <div id="multimedia" class="data new dropzone">

   </div>
   <div class="btn-new-bottom">
      <a href="#new-intervention">Criar Intervenção</a>
   </div>
{!! Form::close() !!}

在jQuery中,我把這段代碼:

$("div#multimedia").dropzone({
          url: "/create-intervention",
          paramName: "file", // The name that will be used to transfer the file
          maxFilesize: 1024,
          autoProcessQueue: false
        });

要提交表單,我需要提交一個jquery函數。 在控制器中,我嘗試獲取$files[] = Input::file('file'); 但這返回null。

控制器:

public function store(Request $request)
    {
      $rules = array(
      );

      // do the validation ----------------------------------
      // validate against the inputs from our form
      $validator = Validator::make(Input::all(), $rules);

      // check if the validator failed -----------------------
      if ($validator->fails())
      {

          // get the error messages from the validator
          $messages = $validator->messages();

          return redirect()->back()->withErrors($validator)->withInput();
      }
      else
      {
        $files[] = Input::file('file');

        var_dump($files);
      }
};

我怎樣才能做到這一點? 我想使用dropzone上傳多個文件,但是只是在提交表單時。 在控制器中,我必須將每個文件保存在目錄中,並將文件名保存在數據庫中。

謝謝

您也可以嘗試循環獲取“請求”中的文件:

控制器:

public function store(Request $request)
{
  $rules = array();

  // do the validation ----------------------------------
  // validate against the inputs from our form
  $validator = Validator::make(Input::all(), $rules);

  // check if the validator failed -----------------------
  if ($validator->fails())
  {
      // get the error messages from the validator
      $messages = $validator->messages();

      return redirect()->back()->withErrors($validator)->withInput();
  }
  else
  {
    foreach( $request->file('file') as $file ){        

            $filename = $file->store('file');
            #saves the file
            $array_images_names[] = $filename;
    }

    var_dump($files);
  }
};

JavaScript(允許接受多個文件)

    var post_url_images = $('#post_url_images').val(); 
    Dropzone.options.frm_drop_images = {
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 1024,
    // The configuration we've talked about above
    url: post_url_images,
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 100,
    maxFiles: 100,

    // The setting up of the dropzone
    init: function() {
        var myDropzone = this;

        // First change the button to actually tell Dropzone to process the queue.
        this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
        // Make sure that the form isn't actually being sent.
        e.preventDefault();
        e.stopPropagation();
        myDropzone.processQueue();
        });

        // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
        // of the sending event because uploadMultiple is set to true.
        this.on("sendingmultiple", function() {
        // Gets triggered when the form is actually being sent.
        // Hide the success button or the complete form.
        });
        this.on("successmultiple", function(files, response) {
        // Gets triggered when the files have successfully been sent.
        // Redirect user or notify of success.
        });
        this.on("errormultiple", function(files, response) {
        // Gets triggered when there was an error sending the files.
        // Maybe show form again, and notify user of error
        });
    }

  };

嘗試使用您的HTML表單,如下所示:

<div class="dropzone dropzone-previews" id="frm_drop_images" class="dropzone" method="post" action="{{ url('/admin/products/upload/image') }}"></div>
<!-- hiddent field to set where to post the images -->
<input type="hidden" name="post_url_images" id="post_url_images" value="{{ url('/create-intervention') }}" >
<div class="btn-new-bottom">
<a href="#new-intervention">Criar Intervenção (Não precisa usar este botão)</a>
</div>

添加到表單打開方法'files' => true

文件資料

暫無
暫無

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

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