在MVC 2.0中使用ajax保存图像

时间:2021-10-15 21:22:38

I am having this application where I need to upload images, and I am doing so using AJAX. When I am trying to upload the image using:

我有这个应用程序,我需要上传图像,我正在使用AJAX。当我尝试使用以下内容上传图像时:

<input type="submit"/>

there is no problem, but when I am trying to use,

没有问题,但是当我尝试使用时,

<input type="button"/>

it is causing problem.
In my VIEW, i have something like this:

它引起了问题。在我的视图中,我有这样的事情:

<input type="file" id="OriginalLocation" name="OriginalLocation"/>

And this is what I have put in Controller:

这就是我在Controller中所做的:

public ActionResult SaveEvent(EventModel viewModel, int? page)

{

int Id = Session["ID"] != null ? UtilityHelper.GetIntegerValue(Session["ID"].ToString()) : 0; //this ID we are fetching from session
HttpPostedFileBase file = Request.Files["OriginalLocation"];
viewModel.ContentType = file.ContentType;
Int32 length = file.ContentLength;
byte[] tempImage = new byte[length];
file.InputStream.Read(tempImage, 0, length);
viewModel.ActualImage = tempImage;
// BusinessLayer.Event.BusinessLayer.SetImage(viewModel);
BusinessLayer.Event.BusinessLayer.SaveEvent(viewModel, Id);
EventModel viewmodel = BusinessLayer.TeamEvent.BusinessLayer.GetAllEvents(page, Id);
return View("~/Views/Home/Event.aspx", viewmodel);
}

1 个解决方案

#1


1  

You cannot upload files using AJAX, at least not in older browsers that do not support the HTML 5 File API. If you do not need to support those legacy browsers you could simply use the new FormData and XMLHttpRequest objects. If on the other hand you need to support legacy browsers you might need to resort to some other techniques such as hidden iframes or Flash movies. For example there are plugins that detect the browser capabilities and based on them will use the correct technique. Take a look at the jquery.form plugin or Uploadify.

您不能使用AJAX上传文件,至少不能在不支持HTML 5 File API的旧浏览器中上传文件。如果您不需要支持这些旧版浏览器,则只需使用新的FormData和XMLHttpRequest对象即可。另一方面,如果您需要支持旧版浏览器,则可能需要采用其他一些技巧,例如隐藏的iframe或Flash电影。例如,有些插件可以检测浏览器功能,并且基于它们将使用正确的技术。看一下jquery.form插件或Uploadify。

#1


1  

You cannot upload files using AJAX, at least not in older browsers that do not support the HTML 5 File API. If you do not need to support those legacy browsers you could simply use the new FormData and XMLHttpRequest objects. If on the other hand you need to support legacy browsers you might need to resort to some other techniques such as hidden iframes or Flash movies. For example there are plugins that detect the browser capabilities and based on them will use the correct technique. Take a look at the jquery.form plugin or Uploadify.

您不能使用AJAX上传文件,至少不能在不支持HTML 5 File API的旧浏览器中上传文件。如果您不需要支持这些旧版浏览器,则只需使用新的FormData和XMLHttpRequest对象即可。另一方面,如果您需要支持旧版浏览器,则可能需要采用其他一些技巧,例如隐藏的iframe或Flash电影。例如,有些插件可以检测浏览器功能,并且基于它们将使用正确的技术。看一下jquery.form插件或Uploadify。