[英]Upload a picture using ASP.NET MVC4 RAZOR
I am using kendo mobile to build a mobile application in which the user will be able to click and upload a photo. 我使用kendo mobile构建了一个移动应用程序,用户可以在其中单击并上传照片。 When they first enter the page, it will show their current photo, I want to be able to click and open file explorer on their device and have the ability to show a preview of their photo in place of the old one. 当他们第一次进入页面时,它将显示他们的当前照片,我希望能够单击并在他们的设备上打开文件浏览器,并能够显示照片的预览来代替旧照片。 Then when the click done it will send it to my MVC controller where I can then send it to where I want. 然后,单击完成后,它将被发送到我的MVC控制器,然后我可以将其发送到我想要的位置。 I cant figure out how to send my file to the controller. 我不知道如何将文件发送到控制器。
HTML 的HTML
<div id="NewAccountUploadContainer">
<img id="NewAccountUpload" src="~/Images/btnCamera.png" data-bind="click: uploadPhoto" />
@using (Html.BeginForm("SendNewPhoto", "MobilePlatform", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="ImageUploadBtn" style="display: none" type="file" accept="image/*" />
<input type="submit" value="OK" style="display: none" />
}
<div id="ImgUploadTxt" data-bind="click: uploadPhoto">
Upload a<br />
different photo.
</div>
The #ImageUploadBtn will be triggered by the #NewAccountUpload or #ImgUploadTxt clicks in jquery which works, but I cant get it to display a file or send to my controller when I trigger the submit. #ImageUploadBtn将由有效的jquery中的#NewAccountUpload或#ImgUploadTxt单击触发,但触发触发提交时却无法显示该文件或将其发送到我的控制器。
C# Controller C#控制器
[HttpPost]
public ActionResult SendNewPhoto(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
The file is always null at this point. 此时文件始终为null。
I'm using the Kendo for mvc4, and a mobile implementation, and I'm using the follow code, works for me: 我正在将Kendo用于mvc4和移动实现,并且正在使用以下代码对我有效:
View: @(Html.Kendo().Upload() .Name("files") ) 查看:@(Html.Kendo()。Upload().Name(“ files”))
Controller 控制者
public ActionResult Submit(IEnumerable<HttpPostedFileBase> files)
{
if (files != null)
{
TempData["UploadedFiles"] = GetFileInfo(files);
}
return RedirectToAction("Result");
}
public ActionResult Result()
{
return View();
}
private IEnumerable<string> GetFileInfo(IEnumerable<HttpPostedFileBase> files)
{
return
from a in files
where a != null
select string.Format("{0} ({1} bytes)", Path.GetFileName(a.FileName), a.ContentLength);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.