MVC C#ドライバを呼び出すとAJAXファイルがアップロードされますが、ブラウザはページを更新してリロードします。mvcファイルをアップロードするときにブラウザでリフレッシュするC#
しかし、ドライバにファイルを保存する行にコメントを付けると、ファイルがサーバーに保存されている場合にのみ発生します。 File.SaveAs (fname);
MVCコントローラ:JavaScriptで
[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.");
}
}
コールの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.");
}
}
あなたは、AJAXハンドラを取り付ける際に、デフォルトボタンの動作を提出取り消しますか? – GSerg
は、UploadDocumentの呼び出し方法を示します。 「api_upload_Document」、 URL:「API /アップロード/ドキュメント」、 デフォルト:問題はuploaddocumentメソッドの呼び出しが context.MapRoute( 名で作成されたフレンドリーURLを使用して作られて – ADyson
です可能性が高いことがあります新しい{controller = "Upload"、アクション= "UploadDocument"、id = UrlParameter.Optional}); – Electrofrost