【问题标题】:File uploading with laravel使用 laravel 上传文件
【发布时间】:2013-12-15 01:26:03
【问题描述】:

我对@9​​87654321@ 有疑问。
我正在使用 XHR 发送文件,然后使用 jSon 发送结果。

当我解析 Input::all() 时,我得到一个空结果,但 $_FILES 包含我上传的文件..
我不知道具体该怎么做。

这里是 HTML

 @extends('layouts.master')

    @section('sidebar')
    @parent
    @stop

    @section('bread')
    <div class="container" style="margin-top:10px">
        <ul class="breadcrumbs">
          <li><a href="#">Home</a></li>
          <li><a href="<?php echo action('VideoController@viewall'); ?>">Video</a></li>
          <li class="unavailable"><a href="#">Add new</a></li>
        </ul>
    </div>
    @stop

    @section('content')
<div class="panel">

    <div class="row" style="padding:0;">
        <div class="small-3 columns">
            <div class="panel callout radius">
            <?= Form::open(array('action' =>     
'VideoController@thumbnail_upload', 'files' => true)); ?>
                {{ Form::token() }}
              <a id="bindUpload" class="th"data-tooltip="" class="has-tip" title="Click to upload a new image" >
                <img id="" src="http://image.noelshack.com/fichiers/2013/11/1362996593-pblv.gif">
              </a>
              <div id="progress"></div>
              <?= Form::file('thumbnail', array('style' => 'display:none;', 'id' => 'uploadThumb')); ?>
            <?= Form::close(); ?>
            </div>
        </div>
        <div class="small-9 columns">
            <?php echo Form::open(array('action' => 'VideoController@add', 'data-abide')) ?>

            <div class="panel">
                  <div class="name-field">
                    <label>Titre <small>required</small></label>
                    <?= Form::input('text', 'title', null, array('required pattern' => '[a-zA-Z]+')); ?>
                    <small class="error">Vous devez spécifiez ce champs !</small>
                  </div>
                  <div class="email-field">
                    <label>Réalisateur / Réalisatrice <small>required</small></label>
                     <?= Form::input('text', 'producer', null, array('required pattern' => '[a-zA-Z]+')); ?>
                    <small class="error">Dieu n'a pas fais le film tout seul !</small>
                  </div>
                  <div class="name-field">
                    <label>Description <small>required</small></label>
                    <?= Form::textarea('text', null, array('required pattern' => '[a-zA-Z]+', 'style' => 'height:125px')); ?>
                    <small class="error">Vous devez spécifiez ce champs !</small>
                  </div>
                  <div class="name-field">
                    <label>URL <small>required</small></label>
                    <?= Form::url('url', null); ?>
                    <small class="error">Le champs doit être une URL valide !</small>
                  </div>
                  <div class="name-field">
                    <label>Thumbnail <small>required</small></label>
                    <?= Form::input('text', 'thumbnail', null, array('disabled' => '', 'required')); ?>
                    <small class="error">Vous devez ajouter une photo !</small>
                  </div>

                  <?= Form::submit('Send new video', array('class' => 'medium button green')); ?>
                  <?= Form::close(); ?>
            </div>
        </div>
    </div>
    </div>

    <script>
        $("#bindUpload").click(function() {
            $("#uploadThumb").click();
        });

        $("#uploadThumb").change(function() {

            var fileInput = document.querySelector('#uploadThumb'),
            progress = document.querySelector('#progress');

            var xhr = new XMLHttpRequest();

            xhr.open('POST', '/video/thumbnail_upload');

            xhr.upload.onprogress = function(e) {
                progress.value = e.loaded;
                progress.max = e.total;
            };

            xhr.onload = function(data) {
                console.log(data);
                alert('Upload terminé !');
            };

            xhr

            var form = new FormData();
            form.append('file', fileInput.files[0]);

            xhr.send(form);
        });
    </script>

    @stop 

这里是php

    public function thumbnail_upload() {
    if (!Request::ajax())
    {
        return Response::json(Input::all());
    }

提前谢谢..

【问题讨论】:

    标签: file file-upload upload laravel laravel-4


    【解决方案1】:

    试试

    Input::file('file')->getRealPath();
    

    等等

    http://laravel.com/docs/requests#files

    【讨论】:

      【解决方案2】:

      当然$_POST 是空的。你没有发送任何东西。

      var form = new FormData();
      // This just appends file
      form.append('file', fileInput.files[0]);
      // and you send the file only
      xhr.send(form);
      

      所以在xhr.send(form) 之前添加这样的内容并测试

      form.append('message', 'hello world');
      

      【讨论】:

        【解决方案3】:

        问题已解决。它没有返回任何东西,因为我是 json 编码对象而不是数组。

        感谢您的帮助

        【讨论】:

          猜你喜欢
          • 2022-01-12
          • 2019-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-26
          • 2016-08-11
          • 2016-04-18
          相关资源
          最近更新 更多