Je cours ASP.NET MVC Beta. J'ai un appel AJAX passant par jQuery qui saisit mes valeurs de formulaire et les envoie à une action de contrôleur. Si je lance cet appel sans AJAX dans un scénario post-formel normal, tout se passe correctement. Cependant, une fois que j'incorpore le code jQuery, toutes les valeurs de formulaire SAUF les fichiers à venir. Request.Files() ne renvoie rien. Y a-t-il une pièce supplémentaire qui me manque ici?Request.Files Collection vide lors du passage de jQuery au code ASP.NET
Code HTML:
<form action="/Images/AddImages/" enctype="multipart/form-data" id="frmAddImages" method="post">
<%=Html.Hidden("hfPromoId",ViewData.Model.Promotion.PromoId) %>
<table>
<tr>
<td>Images for Promo: </td>
<td><span class="promoTitle"><%=Html.Encode(ViewData.Model.Promotion.PromoName) %></span></td>
</tr>
<tr class="newImageContainer">
<td>Select Image:</td>
<td>
<input type="file" id="txtImagePath" name="txtImagePath" />
</td>
</tr>
<tr class="newImageContainer">
<td>Image Caption:</td>
<td>
<input id="txtImageCaption" name="txtImageCaption" />
</td>
</tr>
<tr>
<td><input type="submit" value="Save Image" id="AddImages" /></td>
</tr>
</table>
</form>
Code Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddImages(bool? partial)
{
int promoId = Convert.ToInt32(Request.Form["hfPromoId"]);
var promo = _promoRepository.GetPromotion(promoId);
string imageCaption = Request.Form["txtImageCaption"].ToString();
string imagePath = string.Empty;
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
{ continue; }
imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Content\PromoImages",
promo.PromoId.ToString() + "__" + Guid.NewGuid().ToString() + Path.GetFileName(hpf.FileName));
hpf.SaveAs(imagePath);
}
_promoRepository.SaveImageForPromo(promoId, imageCaption, imagePath, this.Request);
if (partial == true)
{
return List(true, promoId);
}
return View("Images", GetModel(promoId));
}
Code jQuery:
$(document).ready(function() {
$('#frmAddImages').submit(function() {
runAjax(form, updateImageList, 'html');
return false;
});
});
function runAjax(form, callback, format) {
$.ajax({
url: form.action + '?partial=true',
type: form.method,
dataType: format,
data: $(form).serialize(),
success: callback
});
}
function updateImageList(result) {
$('#gallery').html(result);
setElementStyling();
}
Certaines de ces choses sont encore nouvelles pour moi. J'ai utilisé le plugin jQuery Form pour obtenir les résultats dont j'avais besoin. Merci! –