簡體   English   中英

使用ASP.NET MVC4 RAZOR上傳圖片

[英]Upload a picture using ASP.NET MVC4 RAZOR

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

此時文件始終為null。

我正在將Kendo用於mvc4和移動實現,並且正在使用以下代碼對我有效:

查看:@(Html.Kendo()。Upload().Name(“ files”))

控制者

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM