1

I upload image to my base and Storage(this works) but when I try download image I get error illegal chars.

 $(document).on('submit','#formToAjax',function(event){
            event.preventDefault();
            var url=$(this).attr("action");
            $.ajax({
                url: url,
                type: $(this).attr("method"),
                dataType: "JSON",
                data: new FormData(this),
                cache: false,
                processData: false,
                contentType: false,
                success: function (data, status){
                    console.log(data);
                },
                error: function (xhr, desc, err){
                    console.log(xhr);
                    console.log(desc);
                    console.log(err);
                }
            });
        })

And My controler

public function UploadImage() {
    $file = Request::file('filefield');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
    $entry = new Upload();
    $entry->mime = $file->getClientMimeType();
    $entry->original_filename = $file->getClientOriginalName();
    $entry->filename = $file->getFilename().'.'.$extension;

    $entry->save();

    $file = Storage::disk('local')->get($entry->filename);

    return (new Response($file, 200))->header('Content-Type', $entry->mime);
}

And this is error when i try return image from controller:

enter image description here

If I don't use ajax then image normal load.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Piotr Kazuś
  • 346
  • 2
  • 12
  • possible duplicate of [Using jQuery's ajax method to retrieve images as a blob](http://stackoverflow.com/questions/17657184/using-jquerys-ajax-method-to-retrieve-images-as-a-blob) – Joel Hinz May 16 '15 at 08:08

1 Answers1

0

i found results for this. In controller i return not image but filname upload image.

public function UploadImage() {
    $file = Request::file('filefield');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
    $entry = new Upload();
    $entry->mime = $file->getClientMimeType();
    $entry->original_filename = $file->getClientOriginalName();
    $entry->filename = $file->getFilename().'.'.$extension;

    $entry->save();
    return $entry->filename;
}

In my ajax i call to my routes to download image

$(document).on('submit','#formToAjax',function(event){
            event.preventDefault();
            $.ajax({
                url: $(this).attr('action'),
                type: $(this).attr('method'),
                data: new FormData(this),
                cache: false,
                processData: false,
                contentType: false,
                success: function (data, status){
                    $(".test").html('<img src="/admin/upload/get/'+data+'"  class="img-responsive" />');
                }
            });
        })

And this is download image

public function UploadGet($filename){

    $entry = Upload::where('filename', '=', $filename)->firstOrFail();
    if($exists = Storage::disk('local')->exists($entry->filename)){
        $file = Storage::disk('local')->get($entry->filename);
    }else{
        $file = 'dupa';
    }
    return (new Response($file, 200))->header('Content-Type', $entry->mime);
}

maybe it will help someone :)

Piotr Kazuś
  • 346
  • 2
  • 12