Django-Tasypie image upload example with JQuery

僤鯓⒐⒋嵵緔 提交于 2019-12-04 19:23:10
kgr

If you mean using $.ajax() or $.post() then it won't work because Ajax doesn't support file uploads. See e.g. this SO post.

There are some workarounds though - e.g. uploading via form in hidden iframe or flash-based solutions - they are discussed in the SO post mentioned above. None of them are perfect though, so you will have to choose the one which best serves your use case.

In case anybody stumbles upon this in the future, I got image upload working (which was working using curl already) in my case using this jquery File Upload Demo on github.

Use the Basic file upload:

  1. Include necessary js files (jquery, jquery-fileupload, iframe-transport, and jquery.ui.widget).
  2. Initialize the target input[type=file] control as shown below

Code:

$('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('#files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');

This should do the trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!