通过MVC,jQuery,Ajax上传文件

时间:2012-05-23 06:23:39

标签: jquery ajax asp.net-mvc model-view-controller

我通过MVC,jQuery,Ajax创建了文件上传器。但它正在发挥作用 仅适用于1到3千字节的文件,并且不适用于较大的hajn文件。如何上传更大尺寸的文件?

HTML:

<input type="file" name="bfile"  size="50" id="bfile"/>
   <input type="butt

AppAttachmentsController-controller代码:

 public string file_upload(string file_name, string file_binary)
        {

            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(file_binary);
            MemoryStream stream = new MemoryStream(byteArray);
            string path=Server.MapPath(Url.Content("~/upload/file"))+"\\"+file_name;
            using (var fileStream = new FileStream(path, FileMode.Create))
                stream.CopyTo(fileStream);

            return "1";
        }

jQuery,Ajax

function AttachemtPost() {
    var file_name = $("#bfile").val(); 
    var data = "file_name=" + file_name;
    var file = document.getElementById('bfile').files[0];
    var file_binary = file.getAsBinary();
    var file_size = file.fileSize;
    var file_text = file.getAsText("");
    alert('file :' + file_binary);
    alert('file size:' + file_size);
    data = "file_name=" + file_name + "&file_binary=" + file_binary;       
    ajaxlocalcall2(null, "POST", '@Url.Content("~/AppAttachments/file_upload")', data, "", "", "html", "oncontactinfosave");    
}

1 个答案:

答案 0 :(得分:0)

您可以尝试在web.config中增加允许的最大请求大小,默认情况下为4MB:

<httpRuntime maxRequestLength="size in kbytes" />

此外,如果您在IIS 7+中托管应用程序,则应设置request limit

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="size in bytes" />
        </requestFiltering>
    </security>
</system.webServer>