【问题标题】:Upload a picture using ASP.NET MVC4 RAZOR使用 ASP.NET MVC4 RAZOR 上传图片
【发布时间】:2014-04-09 15:30:56
【问题描述】:

我正在使用 kendo mobile 构建一个移动应用程序,用户可以在其中单击并上传照片。当他们第一次进入页面时,它会显示他们当前的照片,我希望能够在他们的设备上单击并打开文件资源管理器,并能够显示他们照片的预览来代替旧照片。然后当点击完成时,它会将它发送到我的 MVC 控制器,然后我可以将它发送到我想要的地方。我不知道如何将我的文件发送到控制器。

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 将由 jquery 中的 #NewAccountUpload 或 #ImgUploadTxt 点击触发,这可以正常工作,但是当我触发提交时,我无法让它显示文件或发送到我的控制器。

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

此时文件始终为空。

【问题讨论】:

    标签: c# asp.net-mvc-4 mobile razor


    【解决方案1】:

    我正在使用用于 mvc4 的 Kendo,以及一个移动实现,并且我正在使用以下代码,对我有用:

    查看: @(Html.Kendo().Upload() .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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2021-01-06
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多