【发布时间】:2018-09-01 11:42:32
【问题描述】:
我有一个这样的脚本,我还想向控制器发送照片 但是当我添加发送文件部分时,我无法将文件发送到控制器。有什么办法可以同时发布文件和数据?
这是我的代码:
$(document).ready(function () {
$("#btnBecKaydet").click(function () {
var formBeceri = $("#FormumBec").serialize();
$.ajax({
type: "POST",
url: "/Cv/BeceriEkle",
data: formBeceri,
success: function () {
$("#ModelimBec").modal("hide");
}
});
});
});
-----脚本-----------
public ActionResult BeceriEkle(kisiselWeb.Models.tbl_beceri s1 , HttpPostedFileBase file)
{
if(file != null)
{
if (System.IO.File.Exists(Server.MapPath(s1.beceriFoto)))
{
System.IO.File.Delete(Server.MapPath(s1.beceriFoto));
}
WebImage img = new WebImage(file.InputStream);
FileInfo fotoinfo = new FileInfo(file.FileName);
string newfoto = Guid.NewGuid().ToString() + fotoinfo.Extension;
img.Resize(75, 75);
img.Save("~/Foto/Beceri/" + newfoto);
s1.beceriFoto = "/Foto/Beceri/" + newfoto;
}
db.tbl_beceri.Add(s1);
db.SaveChanges();
return RedirectToAction("Cv", "Beceri");
}
--控制器--
<div class="modal-body">
<div class="container">
<form id="FormumBec">
<div class="col-md-12 align-content-center">
@Html.Label("Beceri Başlığı : ", htmlAttributes: new { @class = "control-label col-md-6" })
<input type="text" name="beceriBaslik" /><br />
@Html.Label("Beceri Fotoğrafı : ", htmlAttributes: new { @class = "control-label col-md-12" })
<input type="file" id="BecFot" accept=".jpg,.png,.JPEG,.jpeg" /><br />
</div>
</form>
</div>
</div>
【问题讨论】:
标签: javascript asp.net file post model-view-controller