5
votes

I click 'Add files' choose a file it then creates a thumbnail (depending on browser) and displays the 'Upload' button beneath it.

when I click 'Upload', sometimes it uploads the file and says 'File upload failed.' - the file is actually uploaded but I still get this message. Sometimes it doesn't upload and i get the same message.

I have two errors -

  1. 1st is in the PHP error log 'PHP Warning: exif_imagetype(): Filename cannot be empty in ***\***\UploadHandler.php' I don't get it all the time though.

  2. 2nd is jquery.validation on the page - 'Uncaught Error: Syntax error, unrecognized expression: [name=files[]]'

In the debugger it jumps straight to '.on('fileuploadfail', function (e, data)' without going anywhere else and once that completes it refreshes the whole page..guess I need a 'return false' somewhere?

Thanks for your help.

HTML:

<span class="btn btn-success fileinput-button">
    <i class="glyphicon glyphicon-plus"></i>
    <span>Add files...</span>
    <!-- The file input field used as target for the file upload widget -->
    <input id="fileupload" type="file" name="files[]" multiple>
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
    <div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>

Javascript:

var filepower =  function () {
'use strict';
var url = 'server/php/',
    uploadButton = $('<button/>')
        .addClass('btn btn-primary')
        .prop('disabled', true)
        .text('Processing...')
        .on('click', function () {
            var $this = $(this),
                data = $this.data();
            $this
                .off('click')
                .text('Abort')
                .on('click', function () {
                    $this.remove();
                    data.abort();
                });
            data.submit().always(function () {
                $this.remove();
            });
        });
$('#fileupload').fileupload({
    url: url,
    dataType: 'json',
    autoUpload: false,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 5000000, // 5 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true
}).on('fileuploadadd', function (e, data) {
    data.context = $('<div/>').appendTo('#files');
    $.each(data.files, function (index, file) {
        var node = $('<p/>')
                .append($('<span/>').text(file.name));
        if (!index) {
            node
                .append('<br>')
                .append(uploadButton.clone(true).data(data));
        }
        node.appendTo(data.context);
    });
}).on('fileuploadprocessalways', function (e, data) {
    var index = data.index,
        file = data.files[index],
        node = $(data.context.children()[index]);
    if (file.preview) {
        node
            .prepend('<br>')
            .prepend(file.preview);
    }
    if (file.error) {
        node
            .append('<br>')
            .append($('<span class="text-danger"/>').text(file.error));
    }
    if (index + 1 === data.files.length) {
        data.context.find('button')
            .text('Upload')
            .prop('disabled', !!data.files.error);
    }
}).on('fileuploadprogressall', function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    $('#progress .progress-bar').css(
        'width',
        progress + '%'
    );
}).on('fileuploaddone', function (e, data) {
    $.each(data.result.files, function (index, file) {
        if (file.url) {
            var link = $('<a>')
                .attr('target', '_blank')
                .prop('href', file.url);
            $(data.context.children()[index])
                .wrap(link);
        } else if (file.error) {
            var error = $('<span class="text-danger"/>').text(file.error);
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        }
    });
}).on('fileuploadfail', function (e, data) {
    $.each(data.files, function (index, file) {
        var error = $('<span class="text-danger"/>').text('File upload failed.');
        $(data.context.children()[index])
            .append('<br>')
            .append(error);
    });
}).prop('disabled', !$.support.fileInput)
    .parent().addClass($.support.fileInput ? undefined : 'disabled');
};
2
Avoid bigger text in your question.>! - Anup
I know it's a lot but I can't think how else to explain it. - Joe Keene
The fileuploadfail event is triggered when the server response contains a HTTP error status. So the server has receive your request, and the upload can be well done. Do you use a server-side framework? - Fractaliste
I have the same issue . Did you solve this @ch3my - Karem
I am also facing this same problem in rails....there is anyway to solve that.. help me frds - praveenkumar

2 Answers

0
votes

You didn't post your source code, but if you can add to the response on the server, then you can fix it by implementing the return structure here: https://github.com/blueimp/jQuery-File-Upload/wiki/JSON-Response

I can't help you if you can't change the server, though the library author says there's a way to modify/over-ride the expected JSON fields so it doesn't error out.

0
votes

Either return an appropriate expected response from the server or get data.jqXHR . this will contain the response from the server. you can use that to check whether upload was successful or not.