用于在asp.net mvc4中创建,编辑,删除的部分视图页面

时间:2014-02-22 09:53:08

标签: image uploading

如何在asp.net中上传图片mvc4.images保存在本地文件夹中,图片路径名保存到数据库。

1 个答案:

答案 0 :(得分:0)

我想说使用 Valum的AJAX文件上传器

这是我如何做到的

 [HttpGet]
    public ActionResult PictureUpload()
    {
        return View();
    }

    [HttpPost]
    public ActionResult PictureUpload( string qqfile)
    {
        var path = Server.MapPath("~/App_data/Files/");
        var file = string.Empty;

        try
        {
            var stream = Request.InputStream;
            if (String.IsNullOrEmpty(Request["qqfile"]))
            {
                // IE
                HttpPostedFileBase postedFile = Request.Files[0];
                if (postedFile != null) stream = postedFile.InputStream;
                file = Path.Combine(path, qqfile);
            }
            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");
    }

**这个GIVEN HTML和Javascript需要来自Valums网站的uploaderjs文件和css文件**

    

索引

<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="~/Scripts/fileuploader.js"></script>
<script type="text/javascript">

    var uploader = new qq.FileUploader({
        // pass the dom node (ex. $(selector)[0] for jQuery users)
        element: document.getElementById('file-uploader'),
        // path to server-side upload script
        action: '/FileUpload/File',
        onComplete: function(data) {
            alert(data);
        },
    });
</script>

希望这会有所帮助