2014-04-09 109 views
0

我正在使用kendo mobile来构建移动应用程序,用户可以在其中点击并上传照片。当他们第一次进入页面时,它会显示他们当前的照片,我希望能够点击并打开他们设备上的文件浏览器,并且能够显示他们照片的预览而不是旧照片。然后当点击完成后,它会将它发送到我的MVC控制器,然后我可以将它发送到我想要的位置。我不知道如何将我的文件发送到控制器。使用ASP.NET MVC4上传图片RAZOR

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> 

的#ImageUploadBtn将由#NewAccountUpload或触发#ImgUploadTxt点击jQuery中其中的作品,但我不能让它显示文件或文件发送到我的控制器当我触发提交。

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"); 
    } 

文件总是空在这一点上。

回答

0

我使用的是剑道的mvc4和移动实现,以及我使用的后续代码,工作对我来说:

景观: @(Html.Kendo()上传( ) 请将.Name( “文件”) )

控制器

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); 
     }