简体   繁体   English

使用Ajax调用将文件上传到虚拟目录

[英]Upload file to virtual directory using Ajax call

I am building a CMS as MVC 4 project and one of the features is to upload your photo . 我正在将CMS作为MVC 4项目构建,其功能之一就是上传照片 The user chooses a photo from his hard drive, which triggers an ajax request to the UploadFile method on the controller. 用户从他的硬盘驱动器中选择一张照片,这会触发对控制器上UploadFile方法的ajax请求。 This should copy the photo to a virtual folder on the server. 这会将照片复制到服务器上的虚拟文件夹中。 The problem is I don't really understand where the browser stores the file and sends it to the server, and what I am supposed to do on the controller. 问题是我不太了解浏览器将文件存储在何处并将其发送到服务器,以及我在控制器上应该执行的操作。

This is my code so far - 到目前为止,这是我的代码-

The view: 风景:

<input id="cng_pic_btn" type="file" name="file" accept="image/*" /></td>

JavaScript making the call to the server: JavaScript调用服务器:

$('#cng_pic_btn').live('change', function () {

    custom_url = "/SideBar/UploadFile";
    return_msg = "file uploaded";

    var file_path = $('#cng_pic_btn').attr("value");
    alert(file_path);
    sendInfo = {
        upload_from: file_path
    }

    CreataAjaxRequest(custom_url, sendInfo, return_msg);

})

The conroller method: 控制器方法:

    [HttpPost]
    public void UploadFile(string upload_from)
    {
            string path = @"D:\Temp\";

            HttpPostedFileBase photo = Request.Files[upload_from];

            photo.SaveAs(path + photo.FileName);
    }

send ajax request: 发送ajax请求:

function CreataAjaxRequest(custom_url, sendInfo, return_msg) {

    $.ajax({ type: "POST", url: custom_url, data: sendInfo })
                .success(function (html) {
                    $(".query-result").replaceWith(html);

                })

}

You haven't shown your CreataAjaxRequest method but if you want to upload files using AJAX there are a couple of options: 您尚未显示CreataAjaxRequest方法,但如果要使用AJAX上传文件,则有两个选项:

  • your client browser supports the HTML 5 File API in which case you could use the XmlHttpRequest2 object 您的客户端浏览器支持HTML 5 File API在这种情况下,您可以使用XmlHttpRequest2对象
  • your client browser doesn't support the File API (Such as Internet Explorer) in which case you could use a file upload plugin such as Uploadify or Fine Uploader which use techniques like hidden iframes or Flash movies for those kind of legacy browsers. 您的客户端浏览器不支持File API(例如Internet Explorer),在这种情况下,您可以使用文件上传插件(例如UploadifyFine Uploader ),这些插件对这类旧版浏览器使用隐藏的iframe或Flash电影等技术。

Here's an example of how you could upload a file using the HTML 5 File API: 以下是使用HTML 5 File API上传文件的示例:

function CreataAjaxRequest(custom_url, sendInfo, return_msg) {
    var xhr = new XMLHttpRequest();
    var fd = new FormData();
    xhr.open('POST', custom_url, true);
    var file = document.getElementById('cng_pic_btn').files[0];;
    fd.append('myFile', file);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            $('.query-result').replaceWith(xhr.responseText);
        }
    };
    xhr.send(fd);
}

and then on your server: 然后在您的服务器上:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase myFile)
{
    var path = string path = @"D:\Temp\";
    myFile.SaveAs(Path.Combine(path, myFile.FileName));

    return PartialView();
}

Also notice that your controller action needs to return a PartialView if you want to use the $('.query-result').replaceWith(xhr.responseText); 另请注意,如果您想使用$('.query-result').replaceWith(xhr.responseText);则控制器操作需要返回PartialView $('.query-result').replaceWith(xhr.responseText); method in your AJAX callback, otherwise what are you replacing with? AJAX回调中的方法,否则将替换为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM