با عرض سلام
دوستان من یه فرم دارم که یک فایل تصویر هم داره و میخوام این فرم را با تصویرش به صورت یکجا با Jquery ajax به Web api بفرستم و تصویر و اطلاعات رو توی Web api مدیریت و ذخیره کنم چطوری باید این کار رو انجام بدم .اگر کمک کنید ممنون میشم.
مشکل کجاست ؟
میتونید به سادگی ارسال کنید و فقط تنها نکته اینه که برای ارسال فایل باید از POST استفاده کنید
استاد ارسال میکنم ولی جواب نمیگیرم از این کدها استفاده میکنم
$.ajax({ url: '/api/ApiAdminUser/EditProfile', type: 'POST', dataType: 'json', data: $('#form1').serialize(), success: function (data, textStatus, xhr) { alert(data); }, error: function (xhr, textStatus, errorThrown) { alert(textStatus); } });
اینم کد web api
[HttpPost] public string EditProfile([FromBody] SaveEditUser user) { string Result = user.User_Name + " test"; return Result; }
اینم فرم html است
@using (Html.BeginForm("Edit", "Users", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.User_ID) <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_Name, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.EditorFor(model => model.User_Name, new { htmlAttributes = new { @class = "form-control", } }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-user"></span></span> </div> @Html.ValidationMessageFor(model => model.User_Name, "", new { @class = "text-danger" }) </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_Family, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.EditorFor(model => model.User_Family, new { htmlAttributes = new { @class = "form-control" } }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-user"></span></span> </div> @Html.ValidationMessageFor(model => model.User_Family, "", new { @class = "text-danger" }) </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_BirthDay, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> <input aria-describedby="sizing-addon3" class="form-control" type="text" name="User_BirthDay" id="User_BirthDay" value="@(Model.User_BirthDay.ToString("yyyy/MM/dd"))" /> <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-calendar"></span></span> </div> @Html.ValidationMessageFor(model => model.User_BirthDay, "", new { @class = "text-danger" }) </div> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_Sex, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> <select id="User_Sex" name="User_Sex" class="form-control"> <option value="True" @(Model.User_Sex == true ? "selected" : "")>مرد</option> <option value="False" @(Model.User_Sex == false ? "selected" : "")>زن</option> </select> <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-femal"></span></span> </div> </div> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_Tell, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.EditorFor(model => model.User_Tell, new { htmlAttributes = new { @class = "form-control" } }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-phone"></span></span> </div> @Html.ValidationMessageFor(model => model.User_Tell, "", new { @class = "text-danger" }) </div> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_CellPhone, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.EditorFor(model => model.User_CellPhone, new { htmlAttributes = new { @class = "form-control" } }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-mobile"></span></span> </div> @Html.ValidationMessageFor(model => model.User_CellPhone, "", new { @class = "text-danger" }) </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.Country_ID, "کشور", htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.DropDownList("Country_ID", null, htmlAttributes: new { @class = "form-control" }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-home"></span></span> </div> </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.State_ID, "استان", htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.DropDownList("State_ID", null, htmlAttributes: new { @class = "form-control" }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-home"></span></span> </div> </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.City_ID, "شهر", htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.DropDownList("City_ID", null, htmlAttributes: new { @class = "form-control" }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-home"></span></span> </div> </div> <div class=" col-lg-6 col-md-6 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_PostalCode, htmlAttributes: new { @class = "control-label" }) <div class="input-group input-group-sm"> @Html.EditorFor(model => model.User_PostalCode, new { htmlAttributes = new { @class = "form-control" } }) <span class="input-group-addon my-input-icon" id="sizing-addon3"><span class="fa fa-building"></span></span> </div> @Html.ValidationMessageFor(model => model.User_PostalCode, "", new { @class = "text-danger" }) </div> <div class=" col-lg-12 col-md-12 col-sm-12 col-xs-12"> @Html.LabelFor(model => model.User_PostalAddres, htmlAttributes: new { @class = "control-label" }) @Html.EditorFor(model => model.User_PostalAddres, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.User_PostalAddres, "", new { @class = "text-danger" }) </div> <div class=" col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin-bottom:15px;"> @Html.LabelFor(model => model.User_ImageName, htmlAttributes: new { @class = "control-label" }) <input type="file" name="User_ImageName" id="User_ImageName" value="" /> </div> <div class="form-group"> <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding-right:30px;padding-top:10px;"> <input type="button" value="ذخیره اطلاعات" class="btn btn-success btn-sm" onclick="SaveEditProfile()" /> </div> </div> </div> }
این لینک رو ببینید کمکتون میکنه
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)