8

因此,我在 MVC3 中使用 flash 运行时实现了 plupload。

它完美地工作,因为它使用更正操作上传并运行它。但是,我真的很想能够控制响应,并在 plupload 中处理它,但我似乎无法得到任何响应。

我试过覆盖fileUploaded,但我似乎无法从论点中得到任何东西。我试过返回简单的字符串、json 和你有什么。我似乎无法在客户端得到任何东西。当然,通过闪存发送,我什至无法使用 firebug 调试请求:/

与错误事件相同,并引发异常。它正确地将异常解释为错误,但始终是带有 2038 之类的代码的 #IO ERROR 或另一端出现的东西。我根本无法显示我的异常字符串或任何东西。任何人都可以帮忙吗?

奖励问题:我如何将会话/cookie 数据与 plupload 一起发送,以便我可以在我的操作中访问会话?

4

2 回答 2

13

以下对我有用:

[HttpPost]
public ActionResult Upload(int? chunk, string name)
{
    var fileUpload = Request.Files[0];
    var uploadPath = Server.MapPath("~/App_Data");
    chunk = chunk ?? 0;
    using (var fs = new FileStream(Path.Combine(uploadPath, name), 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);
    }
    return Json(new { message = "chunk uploaded", name = name });
}

在客户端:

$('#uploader').pluploadQueue({
    runtimes: 'html5,flash',
    url: '@Url.Action("Upload")',
    max_file_size: '5mb',
    chunk_size: '1mb',
    unique_names: true,
    multiple_queues: false,
    preinit: function (uploader) {
        uploader.bind('FileUploaded', function (up, file, data) {
            // here file will contain interesting properties like 
            // id, loaded, name, percent, size, status, target_name, ...
            // data.response will contain the server response
        });
    }
});

就奖金问题而言,我愿意通过不使用会话来回答它,因为它们不能很好地扩展,但是因为我知道你可能不喜欢这个答案,所以你有可能通过会话请求中的 id 使用multipart_params

multipart_params: {
    ASPSESSID: '@Session.SessionID'
},

然后在服务器上执行一些 hack以创建正确的会话。

于 2011-05-23T08:27:41.850 回答
5

看这里:

$("#uploader").pluploadQueue({
         // General settings
         runtimes: 'silverlight',
         url: '/Home/Upload',
         max_file_size: '10mb',
         chunk_size: '1mb',
         unique_names: true,
         multiple_queues: false,

         // 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" }
        ],

         // Silverlight settings
         silverlight_xap_url: '../../../Scripts/upload/plupload.silverlight.xap'
      });

      // Client side form validation
      $('form').submit(function (e) {
         var uploader = $('#uploader').pluploadQueue();

         // Files in queue upload them first
         if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind('StateChanged', function () {
               if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                  $('form')[0].submit();
               }
            });

            uploader.start();
         } else {
            alert('You must queue at least one file.');
         }

         return false;
      });

在控制器中:

[HttpPost]
public string Upload(  ) {
          HttpPostedFileBase FileData = Request.Files[0];

          if ( FileData.ContentLength > 0 ) {
             var fileName = Path.GetFileName( FileData.FileName );
             var path = Path.Combine( Server.MapPath( "~/Content" ), fileName );
             FileData.SaveAs( path );
          }

          return "Files was uploaded successfully!";
       }

就是这样......控制器中不需要任何块......

于 2011-12-29T07:36:07.017 回答