Я пытаюсь использовать сценарий, найденный на http://valums.com/ajax-upload/
Мой контроллер выглядит следующим образом
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using MHNHub.Areas.ViewModels;
using MHNHub.Models;
using MHNHub.ViewModels;
namespace MHNHub.Areas.Admin.Controllers
{
[Authorize(Roles = "Administrator")]
public class ImageController : Controller
{
private MHNHubEntities _entities = new MHNHubEntities();
//
// GET: /Image/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult ImageUploader()
{
var viewModel = new ImageViewModel()
{
Image = new Image()
};
return PartialView(viewModel);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImageUploader(Image image)
{
try
{
_entities.Images.AddObject(image);
_entities.SaveChanges();
return RedirectToAction("Index", "Product");
}
catch (Exception ex)
{
var viewModel = new ImageViewModel()
{
Image = image,
HasError = true,
ErrorMessage = ex.Message
};
return PartialView(viewModel);
}
}
private string _uploadsFolder = HostingEnvironment.MapPath("~/App_Data/Files");
public Guid Upload(HttpPostedFileBase fileBase)
{
var identifier = Guid.NewGuid();
fileBase.SaveAs(GetDiskLocation(identifier));
return identifier;
}
private string GetDiskLocation(Guid identifier)
{
return Path.Combine(_uploadsFolder, identifier.ToString());
}
}
}
И у меня есть частичное представление, например this
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Drag and drop files supported in Firefox and Google Chrome with javascript enabled.
<%: Html.TextBoxFor(model => model.Image.FileName) %>
<%: Html.ValidationMessageFor(model => model.Image.FileName)%>
Upload Image
У меня есть файлы fileuploader.js и fileuploader.css, правильно связанные на главной странице, и загрузчик отображается правильно и даже вызывает мое действие, но HttpPostedFileBase имеет значение null, и действие загрузки вызывает исключение. Любое понимание того, что мне делать?
Edit
Итак, я выяснил, используя firebug, что он отправляет XmlHttpRequest. Как мне справиться с этим в моем действии загрузки?