简体   繁体   English

如何在ASP.NET剃须刀中上传文件

[英]How to upload file in ASP.NET razor

I want to upload a file in folder image. 我想上传文件夹图像中的文件。
I use ASP.NET with MVC4 and razor. 我将ASP.NET与MVC4和剃须刀一起使用。

I have this in my View : 我的观点是这样的:

    <div class="editor-label">
         @Html.Label("Descriptif")
    </div>
    <div class="editor-field">
         <input type="file" name="fDescriptif" />
         @Html.ValidationMessageFor(model => model.fDescriptif)
    </div>
[...]
    <p>
        <input type="submit" value="Create" />
    </p>

In my controller : 在我的控制器中:

[HttpPost]
public ActionResult Create(formation formation)
{
    if (ModelState.IsValid)
    {
        db.formations.Add(formation);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(formation);
}

In my model : 在我的模型中:

 public string fDescriptif { get; set; }

I have no idea what I must do to upload my file in my folder "image" and save just the name of my file in my database. 我不知道要怎么做才能将文件上传到“ image”文件夹中,而只将文件名保存在数据库中。 When I validate my form it's my complete path who saved. 当我验证表单时,这是我保存的完整路径。

On your view make sure you have added enctype = "multipart/form-data" to your form tag, and then try like this: 在您的视图上,请确保已将enctype = "multipart/form-data"form标签,然后尝试如下操作:

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmID" }))

And you can Get your file in Controller Action AS: 您可以在Controller Action AS中获取文件:

[HttpPost]
 public ActionResult Create(formation formation,HttpPostedFileBase file)
 {
   if (ModelState.IsValid)
   {
     // Get your File from file
   }

   return View(formation);
 }

you can find many questions like this, even on SO many answers have been given for this topic. 您甚至可以找到很多类似这样的问题,即使对此主题给出了很多答案。

Here are 2 of the links. 这里是2个链接。 you should find your answer in this links. 您应该在此链接中找到答案。 and many more other answers are there for this on SO. SO还有更多其他答案。 just google it, and you will find them. 只是谷歌它,你会发现他们。

https://stackoverflow.com/a/5193851/1629650 https://stackoverflow.com/a/5193851/1629650

https://stackoverflow.com/a/15680783/1629650 https://stackoverflow.com/a/15680783/1629650

Your form doesn't contain any input tag other than the file so in your controller action you cannot expect to get anything else than the uploaded file (that's all that's being sent to the server). 您的表单除了文件之外不包含任何输入标签,因此在控制器操作中,除了上载的文件(发送到服务器的所有内容)之外,您别无所求。 One way to achieve this would be to include a hidden tag containing the id of the model which will allow you to retrieve it from your datastore inside the controller action you are posting to (use this if the user is not supposed to modify the model but simply attach a file): 实现此目的的一种方法是包括一个包含模型ID的隐藏标签,该标签可让您从要发布到的控制器动作内的数据存储中检索它(如果用户不应该修改模型,而是使用该模型,只需附加一个文件):

@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))

 @Html.TextBoxFor(m => m.File, new { @type = "file" })

AND IN CONTROLLER CONTROLLER

[HttpPost]
    public ActionResult ACTION(ViewModels.Model taskModel)
    {
      string path = @"D:\Projects\TestApps\uploadedDocs\";
         if (taskModel.File != null) {
                taskModel.File.SaveAs(path +taskModel.TaskTitle 
                            + taskModel.File.FileName);
                 newUserProject.DocumentTitle = taskModel.TaskTitle 
                            + taskModel.File.FileName;
                  newUserProject.DocumentPath = path + taskModel.File.FileName;
                    }
}
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <input type="submit" />
</form>

And your controller should be as below 和你的控制器应该如下

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {

        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }

        return RedirectToAction("Index");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM