当我将文件上传到iis7时文件被切断

时间:2018-04-30 08:55:52

标签: javascript asp.net

我想通过带有ajax的formdata将一些文件上传到iis 7,但它们被削减到小于80kb,而在调试模式下它可以正常

第一次运行IIS时,它只能运行一次。

Up.html 的源代码,我删除了所有无用的功能:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title></title>
    <script src="../../js/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function UploadFiles() {
            var formData = new FormData();
            var files = $('#fileExcel')[0].files;
            for (var i = 0; i < files.length; i++) {
                formData.append("file[]", files[i]);
            }
            $.ajax({
                url: 'Up.ashx',
                type: 'POST',
                data: formData,
                async: false,
                cache:false,
                processData: false,
                contentType: false,
                success: function() {
                    formData = null;
                }
            });
        }
    </script>
</head>
<body>
<form action="Up.ashx" method="post"></form>
<input id="fileExcel" name="file" type="file" multiple="multiple"/>
<button id="btnUpload" onclick="UploadFiles()">上传</button>
</body>
</html>

还有 Up.ashx 的代码,我删除了所有无用的功能:

using System.IO;
using System.Web;

public class Up : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        HttpFileCollection files = context.Request.Files;
        if (files.Count<1)
        {   
            context.Response.Write("no file");
            context.Response.End();
        }
        string category = this.GetType().ToString();
        string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/" + category + "/");
        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
        }
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            file.SaveAs(filePath+file.FileName);
        }
        context.Response.Write(files.Count + " files");
        context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当我修改这样的代码时,它可以工作,我只是不知道为什么。

public Dictionary<string,string> ReceiveFiles(HttpContext context)
{
    // return files info as Dictionary
    Dictionary<string,string> result = new Dictionary<string, string>();
    string category = this.GetType().ToString();
    string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/" + category + "/");
    if (!Directory.Exists(filePath))
    {
        Directory.CreateDirectory(filePath);
    }
    // the key statement,I just don't know why it works
    string savePath = context.Request["path"];//"path" can be instead to any keystring,T^T!
    string[] files = context.Request.Files.AllKeys;
    foreach (string str in files)
    {
        HttpPostedFile file = context.Request.Files.Get(str);
        try
        {
            file.SaveAs(filePath + file.FileName);
            result.Add(file.FileName,filePath+file.FileName);
            //context.Response.Write("上传成功!");
        }
        catch (Exception ex)
        {
            context.Response.Write("上传失败!错误信息:" + ex.Message.ToString());
            return null;
        }
    }
    return result;
}