asp.net 另一个页面上的文件输入 post(跨页面脚本文件上传使用 ajax)

asp.net file input post on another page (cross page scripting file upload using ajax)

file.aspx

<form action="data/exec.aspx" method="post" enctype="multipart/form-data">
    <input type="file" name="image" required="required" id="image"/>
    <button type="submit">submit</button>
</form>

exec.aspx

HttpPostedFile file = Request.Files["image"];
if (file != null && file.ContentLength > 0 )
{
    try
    {
        string fname = Path.GetFileName(file.FileName);
        file.SaveAs(Server.MapPath(Path.Combine("../images/" + fname)));
        Response.Write("done " + fname);
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}

当我提交表单时,没有任何反应,文件为空。我也试过 asp:file 上传,但遇到了同样的问题。

类似代码适用于 exec.php 页 $_FILES["image"]["name"] 上的 php 示例它 post 的图像并保存,但它在 ASP.NET 中不起作用跨页 post.

 <form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input type="file" name="doc" id="doc" />
    <button type="submit">Submit</button>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script>
    $("form#form1").submit(function () {

        var formData = new FormData($(this)[0]);

        $.ajax({
            url: 'recive.aspx',
            type: 'POST',
            data: formData,
            async: false,
            success: function (data) {
                alert(data)
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    });
</script>

我正在使用 jquery 和 ajax 来 post 来自另一种形式的数据

recive.aspx

HttpPostedFile file = Request.Files["doc"];
        if(file!=null)
        {
            string fname = Path.GetFileName(file.FileName);
            file.SaveAs(Server.MapPath(Path.Combine(fname)));
            Response.Write("done " + fname);
        }
        else
        {
            Response.Write("file empty");
        }