SWFUpload 是如何工作的?

发布于 2024-10-19 01:59:15 字数 2965 浏览 2 评论 0 原文

我下面的代码有效,除了上传部分。到目前为止,我可以毫无问题地加载脚本,并且当用户单击上传按钮时,文件对话框将打开。

但是,我想做的是将文件分配给包含其他文本输入和文本区域元素的表单。

基本上我正在尝试这样做:

  1. 用户用值填充表单(即填充文本输入、文本区域字段)
  2. 用户使用 SWFUpload 选择文件
  3. 用户提交表单

是否可以将文件挂钩到文件输入元素那么当手动提交表单时,所有值都会一次性发送吗?

据我所知,SWFUpload 用于在后台自动提交文件......这可以在一种表单上手动完成吗?

     $('.swfupload-control').swfupload({

        // File Upload Settings
        file_size_limit : "102400", // 100MB
        file_types : "*.*",
        file_types_description : "All Files",
        file_upload_limit : "10",
        file_queue_limit : "0",

        // Button Settings
        button_image_url : "/images/upload_song.gif", // Relative to the SWF file
        button_placeholder_id : "spanButtonPlaceholder",
        button_width: 186,
        button_height: 32,

        // Flash Settings
        flash_url : "/javascripts/swfupload/swfupload.swf"

    });


    $('.swfupload-control')
        .bind('swfuploadLoaded', function(event){
            $('#log').append('<li>Loaded</li>');
        })
        .bind('fileQueued', function(event, file){
            $('#log').append('<li>File queued - '+file.name+'</li>');

          //$('#song_attachment').val(file);

          $('#update-media').parents('li').find('p').remove();
          $('#update-media').parents('li').append('<p>'+(file.name.length >= 45 ? file.name.substr(0, 45) + '...' : file.name)+'</p>');
            // start the upload since it's queued
            //$(this).swfupload('startUpload');
        })
        .bind('fileQueueError', function(event, file, errorCode, message){
            $('#log').append('<li>File queue error - '+message+'</li>');
        })
        .bind('fileDialogStart', function(event){
            $('#log').append('<li>File dialog start</li>');
        })
        .bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
            $('#log').append('<li>File dialog complete</li>');
        })
        .bind('uploadStart', function(event, file){
            $('#log').append('<li>Upload start - '+file.name+'</li>');
        })
        .bind('uploadProgress', function(event, file, bytesLoaded){
            $('#log').append('<li>Upload progress - '+bytesLoaded+'</li>');
        })
        .bind('uploadSuccess', function(event, file, serverData){
            $('#log').append('<li>Upload success - '+file.name+'</li>');
        })
        .bind('uploadComplete', function(event, file){
            $('#log').append('<li>Upload complete - '+file.name+'</li>');
            // upload has completed, lets try the next one in the queue
            $(this).swfupload('startUpload');
        })
        .bind('uploadError', function(event, file, errorCode, message){
            $('#log').append('<li>Upload error - '+message+'</li>');
        });

My code below works, except for the upload part. So far I am able to load the script without any problems, and the file dialogue opens when the a user clicks the upload button.

However, what I am trying to do is to assign the file to a form which contains other text input and text-area elements.

Basically I am trying to do this:

  1. User fills the form up with values (i.e. filling up text input, text-area fields)
  2. User selects a file using SWFUpload
  3. User submits the form

Is it possible to hook the file to a file input element on the form so when the form is submitted manually, all the values are sent on one go?

As far as I know, SWFUpload is used to submit the file automatically on the background on its own... Can this be done manually instead and on one form?

     $('.swfupload-control').swfupload({

        // File Upload Settings
        file_size_limit : "102400", // 100MB
        file_types : "*.*",
        file_types_description : "All Files",
        file_upload_limit : "10",
        file_queue_limit : "0",

        // Button Settings
        button_image_url : "/images/upload_song.gif", // Relative to the SWF file
        button_placeholder_id : "spanButtonPlaceholder",
        button_width: 186,
        button_height: 32,

        // Flash Settings
        flash_url : "/javascripts/swfupload/swfupload.swf"

    });


    $('.swfupload-control')
        .bind('swfuploadLoaded', function(event){
            $('#log').append('<li>Loaded</li>');
        })
        .bind('fileQueued', function(event, file){
            $('#log').append('<li>File queued - '+file.name+'</li>');

          //$('#song_attachment').val(file);

          $('#update-media').parents('li').find('p').remove();
          $('#update-media').parents('li').append('<p>'+(file.name.length >= 45 ? file.name.substr(0, 45) + '...' : file.name)+'</p>');
            // start the upload since it's queued
            //$(this).swfupload('startUpload');
        })
        .bind('fileQueueError', function(event, file, errorCode, message){
            $('#log').append('<li>File queue error - '+message+'</li>');
        })
        .bind('fileDialogStart', function(event){
            $('#log').append('<li>File dialog start</li>');
        })
        .bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
            $('#log').append('<li>File dialog complete</li>');
        })
        .bind('uploadStart', function(event, file){
            $('#log').append('<li>Upload start - '+file.name+'</li>');
        })
        .bind('uploadProgress', function(event, file, bytesLoaded){
            $('#log').append('<li>Upload progress - '+bytesLoaded+'</li>');
        })
        .bind('uploadSuccess', function(event, file, serverData){
            $('#log').append('<li>Upload success - '+file.name+'</li>');
        })
        .bind('uploadComplete', function(event, file){
            $('#log').append('<li>Upload complete - '+file.name+'</li>');
            // upload has completed, lets try the next one in the queue
            $(this).swfupload('startUpload');
        })
        .bind('uploadError', function(event, file, errorCode, message){
            $('#log').append('<li>Upload error - '+message+'</li>');
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-10-26 01:59:15

抱歉,我不知道 SWF 上传,如果不相关,我准备删除这个答案...但是:

我在这里制作了两个应用程序: https://github.com/apneadiving/Pic-upload---Crop-in-Ajax

一个 Uploadify 和一个 jQuery-File-上传

它们都满足您的需求(ajax 提交、多个文件上传)。我推荐 Jquery 文件上传,因为它是无闪存的。

我还在这里写了一个教程: https: //github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-for-Rails-3

Sorry, I don't know SWF Upload and am ready to delete this answer if irrelevant... BUT:

I made two apps here: https://github.com/apneadiving/Pic-upload---Crop-in-Ajax

One with Uploadify and one with jQuery-File-Upload.

They both answer your needs (ajax submitting, multiple files upload). I would recommend Jquery file upload because it's flash-free.

I also wrote a tutorial here: https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-for-Rails-3

软的没边 2024-10-26 01:59:15

不,不可能将 SWFUpload 挂接到文件字段。

浏览器传统上不允许 Javascript 访问用户的文件系统(正在开发 API)。 SWFUpload 通过使用 Flash API 直接从用户的文件系统读取文件并将其流式传输到服务器来解决此问题。为此,Flash 需要直接处理文件系统上的文件,而它无法通过文件输入或 Javascript 获取该文件。

Firefox 对 Javascript File API 有一些支持,请参阅
https://developer.mozilla.org/en/using_files_from_web_applications

No, it's not possible to hook SWFUpload to a file field.

Browsers traditionally don't allow Javascript access to the user's filesystem (there are APIs in the works). SWFUpload gets around this by using Flash APIs to read the file directly from the user's filesystem, and streaming it to a server. To do this Flash needs direct handles to files on the filesystem, which it can't get through the file input or Javascript.

Firefox has some support for the Javascript File API, see
https://developer.mozilla.org/en/using_files_from_web_applications

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文