C MVC 파일을 업로드 내가 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.");
}
}
전화 아약스 :
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.");
}
}
ajax 처리기를 연결할 때 기본 제출 단추 동작을 취소합니까? – GSerg
은 UploadDocument가 호출되는 방법을 보여줍니다. 문제가 context.MapRoute에서 만든 친화적 인 URL을 (사용하여 만들어지는 uploaddocument 방법의 호출 – ADyson
입니다 가능성이 높다 그건 이름 : "api_upload_Document" URL : "API/업로드/문서" 기본값 : 새로운 {controller = "Upload", action = "UploadDocument", id = UrlParameter.Optional}); – Electrofrost