本文介绍了MVC3 Valums阿贾克斯文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用valums AJAX上传。

I'm trying to use valums ajax uploader. http://valums.com/ajax-upload/

我有我的网页上以下内容:

I have the following on my page:

var button = $('#fileUpload')[0];
var uploader = new qq.FileUploader({
    element: button,
    allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
    sizeLimit: 2147483647, // max size
    action: '/Admin/Home/Upload',
    multiple: false
});

它张贴到我的控制器,但qqfile总是空。我想这些:

it does post to my controller but qqfile is always null. I tried these:

public ActionResult Upload(HttpPostedFile qqfile)
AND
HttpPostedFileBase file = Request.Files["file"];

没有任何运气。

我发现了一个榜样Ruby on Rails的,但不知道如何实现它的MVC
http://www.jigsawboys.com/2010/10/06/ruby-on-rails-ajax-file-upload-with-valum/

I found an example for ruby on rails but not sure how to implement it in MVChttp://www.jigsawboys.com/2010/10/06/ruby-on-rails-ajax-file-upload-with-valum/

在萤火我看到这一点:
<一href=\"http://localhost:61143/Admin/Home/Upload?qqfile=2glonglonglongname+-+Copy.gif\">http://localhost:61143/Admin/Home/Upload?qqfile=2glonglonglongname+-+Copy.gif


推荐答案

我想通了。这部作品在IE和Mozilla。

I figured it out. this works in IE and Mozilla.

    [HttpPost]
    public ActionResult FileUpload(string qqfile)
    {
        var path = @"C:\\Temp\\100\\";
        var file = string.Empty;

        try
        {
            var stream = Request.InputStream;
            if (String.IsNullOrEmpty(Request["qqfile"]))
            {
                // IE
                HttpPostedFileBase postedFile = Request.Files[0];
                stream = postedFile.InputStream;
                file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName));
            }
            else
            {
                //Webkit, Mozilla
                file = Path.Combine(path, qqfile);
            }

            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            System.IO.File.WriteAllBytes(file, buffer);
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message }, "application/json");
        }

       return Json(new { success = true }, "text/html");
    }

这篇关于MVC3 Valums阿贾克斯文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 23:34