gpt4 book ai didi

jquery - 使用 Jquery 发布图像

转载 作者:行者123 更新时间:2023-12-01 01:31:16 25 4
gpt4 key购买 nike

我想将带有表单数据的图像发布到服务器,我正在使用“序列化”从表单获取数据,但在那里找不到图像,我找到了一些带有表单提交的示例。但我不想使用提交按钮。这是我的代码,

HTML

        <div asp-validation-summary="ModelOnly" class="text-danger"></div>

<div class="col-md-8">

<div class="col-md-4">
<div class="form-group label-floating">
<label asp-for="Player_Name" class="control-label">Player Name<i class="fa fa-asterisk" style="color: red" aria-hidden="true"></i></label>
<input asp-for="Player_Name" required class="form-control" />
<span asp-validation-for="Player_Name" class="text-danger"></span>
</div>
</div>

</div>
<div class="col-md-4">
<ul class="gallery">
<li><a href="" title="Player Image"><img class="img-responsive" id="PlayerImg" style="background-size:cover" /></a></li>
<li>
<label asp-for="PlayerImage" class="btn btn-box-tool">
<i class="far fa-picture-o"> Upload</i> <input asp-for="PlayerImage" type="file" class="hidden" style="height:auto" />
</label>
</li>
</ul>
</div>
</form>

Jquery

  $(function () {


$('#PlayerCreate').click(function () {
try {
var formData = $("#PlayerForm").serialize();
$.post("/Players/Create", {
data: formData,
headers: {
'RequestVerificationToken': document.getElementById("RequestVerificationToken").value,
},
}).done(function (data) {
window.location = '/Players/Index'
myFunction(data);
}).fail(function (data) {
myFunction(data);
});
} catch (ex) {
console.error(ex);
}
});
});

最佳答案

我自己找到了答案这是代码jQuery$(document).on('click', '#PlayerCreate', function (e) {

        //e.preventDefault();
//e.stopImmediatePropagation();
var form = $("#PlayerForm").closest("form");
var formData = new FormData(form[0]);
$.ajax({
type: "POST",
data: formData,
headers: {
'RequestVerificationToken': document.getElementById("RequestVerificationToken").value,
},
dataType: "json",
url: "/Players/Create",
processData: false,
contentType: false,
success: function (data) {
// myFunction(data);
window.location = '/Players/Index'
},
fail: function (data) {
//myFunction(data);
}
})
});

关于jquery - 使用 Jquery 发布图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com