在ASP.NET MVC 5中上传文件 [英] File Upload in ASP.NET MVC 5

查看:91
本文介绍了在ASP.NET MVC 5中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法上传文件夹中的文件.我找不到错误.上传文件后,UploadFile视图将在同一视图上返回.

I am unable to upload file in folder. I am not able to find the mistake. The UploadFile View returns on same view after uploading file.

模型类:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }
        public string UploadURL { get; set; }

    }

这是Controller(FileUpload)操作:

Here is the Controller(FileUpload) Action:

public ActionResult UploadFile(HttpPostedFileBase file, Upload upload)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                    upload.UploadURL = "/Content/Uploads/Files/" + file.FileName;
                }
                db.Uploads.Add(upload);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(upload);
        }

在我看来:

@using (Html.BeginForm("UploadFile, "FileUpload", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="control-label col-md-2">
                <label for="file">Upload Image  for Slide:</label>
            </div>
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width:50%" />
            </div>

        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

推荐答案

我尝试了与您相同的代码对我有用.

Hi I have tried your same code its works for me.

控制器

   [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                }
                return RedirectToAction("Index");
            }

            return View("UploadFile");
        }

查看

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="control-label col-md-2">
            <label for="file">Upload Image  for Slide:</label>
        </div>
        <div class="col-md-10">
            <input type="file" name="file" id="file" style="width:50%" />
        </div>

    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

我在Html.BeginForm中的动作名称为"的代码中发现了小错误(缺少双引号)

I have found small mistake in you code in Html.BeginForm in action name " (double quotes is missing)

这篇关于在ASP.NET MVC 5中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆