Plupload with C# - Events?

本文关键字:Events with Plupload | 更新日期: 2023-09-27 18:19:01

我对事件不熟悉,对flash也不熟悉。我已经设法让文件上传到我的文件夹使用Plupload。我正在使用一个通用处理程序(也是新的!)

下面是我的代码:

    <style type="text/css">
    @import url(/plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css);
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script type="text/javascript" src="/plupload/js/plupload.full.js"></script>
<script type="text/javascript" src="/plupload/js/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript">
    $(function () {
        $("#uploader").pluploadQueue({
            // General settings
            runtimes: 'gears,flash,silverlight,browserplus,html5',
            url: '/uploader.ashx',
            max_file_size: '10mb',
            chunk_size: '1mb',
            unique_names: true,
            preinit: attachCallbacks,
            // Resize images on clientside if we can
            //resize: { width: 320, height: 240, quality: 90 },
            // Specify what files to browse for
            filters: [
                { title: "Image files", extensions: "jpg,gif,png" },
                { title: "Zip files", extensions: "zip" }
            ],
            // Flash settings
            flash_swf_url: '/plupload/js/plupload.flash.swf',
            // Silverlight settings
            silverlight_xap_url: '/plupload/js/plupload.silverlight.xap'
        });
        // Client side form validation
        $('form').submit(function (e) {
            var uploader = $('#uploader').pluploadQueue();
            // Validate number of uploaded files
            if (uploader.total.uploaded == 0) {
                // Files in queue upload them first
                if (uploader.files.length > 0) {
                    // When all files are uploaded submit form
                    uploader.bind('UploadProgress', function () {
                        if (uploader.total.uploaded == uploader.files.length)
                            $('form').submit();
                    });
                    uploader.start();
                } else
                    alert('You must at least upload one file.');
                e.preventDefault();
            }

        function attachCallbacks(Uploader) {
        Uploader.bind('FileUploaded', function(Up, File, Response) {
            if ((Uploader.total.uploaded + 1) == Uploader.files.length) {
                window.location = '/display.aspx';
            }
        });
    }
});

然后在我的代码中,我的handler:

public class uploader : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
            string fileName = context.Request["name"] ?? string.Empty;
            HttpPostedFile fileUpload = context.Request.Files[0];
            var uploadPath = context.Server.MapPath(GlobalVariables.UploadPath);
            using (
                var fs = new FileStream(Path.Combine(uploadPath, fileName),
                                        chunk == 0 ? FileMode.Create : FileMode.Append))
            {
                var buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);
                fs.Write(buffer, 0, buffer.Length);
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("Success");
        }
        public bool IsReusable
        {
            get { return false; }
        }
    }

这是工作100%

问题是,当它完成上传时,什么也没有发生。显示文件已上传。

我需要告诉一个新方法失火。也就是说,我需要处理上传的文件。把它们移到正确的文件夹…给他们起个好名字……将它们添加到数据库中,并可能重定向到显示屏幕。

所以,我需要flash控件告诉我的代码做一些事情(一个事件),同时,告诉我的代码它现在可以处理哪些文件(上传的文件列表)。

这可能吗?

编辑:我添加了attachCallbacks方法,它是附加的。它现在重定向到一个页面,但我需要以某种方式将上传的文件列表发送到一个方法(可能是通用处理程序?)并处理这些文件。如何重定向到一个方法?

Plupload with C# - Events?

........
uploader.bind('UploadProgress', function () {
    if (uploader.total.uploaded == uploader.files.length)
    $('form').submit();
});
uploader.bind('UploadComplete', function (up, files) {
    //files are uploaded, call script for each file...etc
});

uploader.start();
..........