上传mvc文件时在浏览器中刷新c#

时间:2017-03-16 09:14:51

标签: c# asp.net-mvc-4 model-view-controller

我通过调用MVC C#驱动程序上传了一个AJAX文件,但浏览器刷新并重新加载页面。

但是,如果我评论在驱动程序中保存文件的行不会发生,只有当文件保存在服务器上时才会发生。 File.SaveAs (fname);

MVC控制器:

[HttpPost]
public ActionResult UploadDocument()
{
    if (Request.Files.Count > 0)
    {
        try
        {
            FileUpdateDto fileModal = new FileUpdateDto();

            HttpFileCollectionBase files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file = files[i];
                string fname;
                DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/Content/Document/" + UserId).ToString());
                if (!directory.Exists)
                {
                    Directory.CreateDirectory(Server.MapPath("~/Content/Document/" + UserId).ToString());
                }
                if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                {
                    string[] testfiles = file.FileName.Split(new char[] { '\\' });
                    fname = testfiles[testfiles.Length - 1];
                }
                else
                {
                    fname = file.FileName;
                }
                var guidnew = Guid.NewGuid();
                fname = Path.Combine(Server.MapPath("~/Content/Document/" + UserId), guidnew + "." + fname.Split('.')[1].ToString());
                fileModal.FileName = fname;
                fileModal.Path = directory.ToString();
                fileModal.DateFileUpload = DateTime.Now;
               file.SaveAs(fname);  //  If I comment this line without refreshing the browser but does not save the file
            }

            return Json(fileModal);
        }
        catch (Exception ex)
        {
            return Json("Error occurred. Error details: " + ex.Message);
        }
    }
    else
    {
        return Json("No files selected.");
    }
}

在JavaScript中调用Ajax:

UploadDocument: function () {
    if (window.FormData !== undefined) {

        var fileUpload = $("#AdviserFileUpload").get(0);
        var files = fileUpload.files;

        var fileData = new FormData();

        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }
        //fileData.append('username', 'Manas');  

        $.ajax({
            url: site.baseUrl + '/Api/Upload/Document',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                __this._AdviserDocumentFile = result;
            },
            error: function (err) {
                alert(err.statusText);
            }
        });
    } else {
        alert("FormData is not supported.");
    }
}

1 个答案:

答案 0 :(得分:4)

我相信我找到了解决方案。原因是Visual Studio&#34;&#34;启用Reload on Save&#34;属性是真的。

转到工具 - 选项 - 网络 - 浏览器重新加载保存 - 启用保存时重新加载并将其设为false。

我正在使用VS2015,这对我有用,希望它也适合你。

Source