私はファイルのアップロードを行っています。私のファイルのアップロードが正常に動作しません
この例では、私がやっているまさにほとんどです:https://stackoverflow.com/a/15680783/5874935
をしかし、何かが私のプロジェクトで起こっており、それが突然動作を停止しました。私は仕事をして問題を解決するために役に立たなかった。私は最近、フォームを送信するために、この機能を使用して、単一ページのアプリケーションに変換:
var ajaxFormSubmit = function (contentDiv, formDiv, modalId, formId, controllerPath) {
$(".loader").show();
$("#".concat(formId)).on("submit", function (e) {
console.log("ajax form submitted");
e.preventDefault(); // prevent standard form submission
var form = $(this);
$.ajax({
url: form.attr("action"),
method: form.attr("method"), // post
data: form.serialize(),
error: function() {
$(".loader").hide();
alert("An error occurred.");
},
success: function (partialResult) {
console.log(partialResult.length);
if (partialResult.length === 0) {
console.log("form archhived");
$("#".concat(modalId)).modal('hide');
//forcing the backdrop to go away, something is wrong with the modal, it needs work.
$('.modal-backdrop').remove();
getManager(controllerPath, contentDiv);
//get gunnery manager
}
else {
console.log("form came back");
$("#".concat(formDiv)).html(partialResult);
$(".loader").hide();
}
}
});
});
}
モデル:
public class person
{
public int id {get;set;}
public string fName {get;set;}
public string lName {get;set;}
public HttpPostedFileBase attachment {get;set;}
}
ここでは私のコントローラ部分である:
if (model.attachment != null)
{
var file = model.attachment;
if (file.ContentLength > 0)
{
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"),Path.GetFileName(file.FileName));
file.SaveAs(path);
System.Diagnostics.Debug.WriteLine(path);
model.attachmentLink = path;
}
}
私はそれを何conincidenceを見つけていません私のファイルのアップロードはこの周辺で動作しなくなりましたが、今注目しています。ファイルをアップロードするにはどうしたらいいですか?
ビューはhtmlヘルパーを使用すると非常に標準的です。
以前は$、ajaxで作業していましたか?あなたが$ .ajaxを追加したときに動作を停止しました。私の知る限り、FormDataを使用しない限り、AJAXでファイルをアップロードすることはできません。 –
AJAXを追加したときに停止しました。私はもともとHTTP投稿 –
を使用していたので、フォームデータを追加して動作させる必要があります。 –