在代码下方用于附加文件。当我从visual studio运行应用程序时它工作正常。我在IIS7中部署了应用程序(在我的机器中)。从部署站点,我无法附加文件。请让我知道如何解决这个问题。 视图:
$("#lnkAddAttachment").click(function (e)
{
oFiles = document.getElementById("txtFile").files[0],
nFiles = oFiles.size;
var selectedFile = document.getElementById("txtFile").files[0];
var size = selectedFile.size;
var name = selectedFile.name;
var type = selectedFile.contentDocument;
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append("file", document.getElementById('txtFile').files[0]);
xhr.open("POST", "/ESignature/getFile/", true);
xhr.send(fd);
xhr.addEventListener("load", function (event) {
}, false);
var url = '@Url.Content("~/")' + "ESignature/Registration";
$('#gridAttachments').load(url + ' #gridAttachments');
$('#txtDescription').val('');
$('#txtFile').val('');
return false;
})
控制器:
[HttpPost]
public ActionResult getFile(HttpPostedFileBase file)
{
BRUser brUser = new BRUser();
AttachedFile attachedFile = new AttachedFile();
attachedFile.FileType = "Passport";
attachedFile.Filename = System.IO.Path.GetFileName(@DateTime.Now.Ticks + file.FileName);
attachedFile.Filesize = (file.ContentLength / 1024);
file.SaveAs(Server.MapPath("~/ESignatureUploads/" + attachedFile.Filename));
attachedFile.Filepath = "~/ESignatureUploads/" + attachedFile.Filename;
if (Session["ESAttachedFiles"] != null)
{
brUser.AttachedFiles = (List<AttachedFile>)Session["ESAttachedFiles"];
}
brUser.AttachedFiles.Add(attachedFile);
Session["ESAttachedFiles"] = brUser.AttachedFiles;
return View("Registration", brUser);
}
[HttpGet]
public ActionResult Registration()
{
BRUser objuser = new BRUser();
if (Session["ESAttachedFiles"] != null)
{
objuser.AttachedFiles = (List<AttachedFile>)Session["ESAttachedFiles"];
TempData.Keep();
}
return View(objuser);
}
答案 0 :(得分:0)
在以下功能中,
$("#lnkAddAttachment").click(function (e)
我从
更改了网址xhr.open("POST", "/ESignature/getFile/", true);
到
xhr.open("POST", "~/ESignature/getFile/", true);
它解决了我的问题。