且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net mvc的AJAX上传解决方案?

更新时间:2022-12-18 09:33:08

我个人喜欢 Valums阿贾克斯上传

更新:

由于在评论部分要求这里有一个如何这可能是与ASP.NET MVC中使用的例子。

As requested in the comments section here's an example of how this could be used with ASP.NET MVC.

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Upload(string qqFile)
    {
        // The upload action will be called by the client control
        // for each file that was selected by the user for upload

        var path = Server.MapPath("~/App_Data");
        var file = Path.Combine(path, qqFile);
        using (var output = System.IO.File.Create(file))
        {
            Request.InputStream.CopyTo(output);
        }
        return Json(new { success = true });
    }
}

查看(〜/查看/主页/ Index.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Ajax Upload demo with ASP.NET MVC</title>
    <link href="@Url.Content("~/Content/fileuploader.css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="file-uploader">       
        <noscript>          
            <p>Please enable JavaScript to use file uploader.</p>
            <!-- or put a simple form for upload here -->
        </noscript>         
    </div>

    <script src="@Url.Content("~/Scripts/fileuploader.js")" type="text/javascript"></script>
    <script type="text/javascript">
        var uploader = new qq.FileUploader({
            element: document.getElementById('file-uploader'),
            action: '@Url.Action("Upload", "Home")'
        });    
    </script>
</body>
</html>