使用laravel上传文件

时间:2013-11-28 22:40:11

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

我对Laravel 4有疑问 我正在使用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());
    }

提前致谢..

3 个答案:

答案 0 :(得分:0)

尝试

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

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

答案 1 :(得分:0)

关闭课程$_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)和test

之前添加类似的内容
form.append('message', 'hello world');

答案 2 :(得分:0)

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

感谢您的帮助