Jquery 上载文件并将其保存在临时文件夹中

Jquery 上载文件并将其保存在临时文件夹中,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我使用以下方法上载文件: [HttpPost] public ActionResult Upload() { string directory = @"C:\Temp\"; HttpPostedFileBase file = Request.Files["file"]; if (file != null && file.ContentLength > 0) { va

我使用以下方法上载文件:

[HttpPost]
    public ActionResult Upload()
    {

        string directory = @"C:\Temp\";

        HttpPostedFileBase file = Request.Files["file"];

        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);

            file.SaveAs(Path.Combine(directory, fileName));
        }
        return RedirectToAction("Index");
    }
下面是我的ajax调用:

$('#uploadButton').on('click', function()
{

    $.ajax({
        type: "POST",
        dataType: 'json',
        url: '@Url.Action("Upload", "Application")',
        timeout: 2000,
        contentType: 'application/json',
        success: function (data) {

            //show content
            alert('Success!')
        }
    });
}
)
我需要发送“Request.files[“file”]”和上传的文件

这是我的代码所在的表格:

<form action="/profile/upload" method="post" enctype="multipart/form-data">

    <div class="form-group">

        <label>File</label>

        <input type="file" name="file" id="file" class="form-control" />
        <br />
        <input type="button" id="uploadButton" value="Upload" />
    </div>
</form>

文件

试试这个:

    $('#uploadButton').on('click', function () {
    var data = new FormData();
    var files = $('[type="file"]').get(0).files;
    // Add the uploaded image content to the form data collection
    if (files.length > 0) {
        data.append("file", files[0]);
    }

    $.ajax({
        type: "POST",
        url: '@Url.Action("Upload", "Application")',
        type: 'POST',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            //show content
            alert('Success!')
        }
    });

})

您的服务器端代码是用什么语言编写的?请为它添加一个标记。您可以更改请求的contentType并重试吗,我看不出您在代码中也是如何读取文件的。HttpPostedFileBase file=request.files[“file”];已返回nullcheck此请求。文件[0]