• <legend id='HDTYn'><style id='HDTYn'><dir id='HDTYn'><q id='HDTYn'></q></dir></style></legend>
      • <bdo id='HDTYn'></bdo><ul id='HDTYn'></ul>
      <i id='HDTYn'><tr id='HDTYn'><dt id='HDTYn'><q id='HDTYn'><span id='HDTYn'><b id='HDTYn'><form id='HDTYn'><ins id='HDTYn'></ins><ul id='HDTYn'></ul><sub id='HDTYn'></sub></form><legend id='HDTYn'></legend><bdo id='HDTYn'><pre id='HDTYn'><center id='HDTYn'></center></pre></bdo></b><th id='HDTYn'></th></span></q></dt></tr></i><div id='HDTYn'><tfoot id='HDTYn'></tfoot><dl id='HDTYn'><fieldset id='HDTYn'></fieldset></dl></div>

      1. <small id='HDTYn'></small><noframes id='HDTYn'>

        <tfoot id='HDTYn'></tfoot>

        ASP.NET MVC 上传图片

        时间:2023-06-03

              <legend id='dUkCY'><style id='dUkCY'><dir id='dUkCY'><q id='dUkCY'></q></dir></style></legend>
              <tfoot id='dUkCY'></tfoot>

              <i id='dUkCY'><tr id='dUkCY'><dt id='dUkCY'><q id='dUkCY'><span id='dUkCY'><b id='dUkCY'><form id='dUkCY'><ins id='dUkCY'></ins><ul id='dUkCY'></ul><sub id='dUkCY'></sub></form><legend id='dUkCY'></legend><bdo id='dUkCY'><pre id='dUkCY'><center id='dUkCY'></center></pre></bdo></b><th id='dUkCY'></th></span></q></dt></tr></i><div id='dUkCY'><tfoot id='dUkCY'></tfoot><dl id='dUkCY'><fieldset id='dUkCY'></fieldset></dl></div>
              • <small id='dUkCY'></small><noframes id='dUkCY'>

                • <bdo id='dUkCY'></bdo><ul id='dUkCY'></ul>
                    <tbody id='dUkCY'></tbody>
                • 本文介绍了ASP.NET MVC 上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我找到了一些代码来执行此操作并尝试将其实现到我的项目中,但到目前为止它一直没有成功.我没有收到任何错误,但我没有看到任何图像存储在 Visual Studio 内的图像目录中.

                  I've found some code to do this and tried to implement it into my project, but so far it has been unsuccessful. I don't get any errors, but I don't see any images being stored in my images directory inside visual studio.

                  查看:

                    @using (Html.BeginForm())
                  {
                      <span>Please enter your story here:</span>
                      <textarea id="testimonial" name="testimonial"></textarea>
                      <button type="submit">Submit</button>
                      <input type="file" name="file" />
                  }
                  

                  控制器:

                  [HttpPost]
                      public ActionResult Create(Testimonials testimonials)
                      {
                          if (Request.Files.Count > 0)
                          {
                              var file = Request.Files[0];
                  
                              if (file != null && file.ContentLength > 0)
                              {
                                  var fileName = Path.GetFileName(file.FileName);
                                  var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                                  file.SaveAs(path);
                              }
                          }
                  
                  
                          TestimonialsContext testContext = new TestimonialsContext();
                          testContext.testimonialContext.Add(testimonials);
                          testContext.SaveChanges();
                          return RedirectToAction("Index");
                      }
                  

                  if 块下方的部分工作正常.这只是将文本区域的内容保存到数据库中.有什么想法吗?我需要对我的模型进行任何更改吗?

                  The part below the if block works fine. That just saves the content of a textarea to the database. Any thoughts? Do I need to make any changes to my model?

                  型号:

                  [Table("Testimonials")]
                  public class Testimonials
                  {
                      public int Id { get; set; }
                      public string Testimonial { get; set; }
                  }
                  

                  上下文类:

                  public class TestimonialsContext:DbContext
                  {
                      public DbSet<Testimonials> testimonialContext { get; set; }
                  }
                  

                  推荐答案

                  您的文件未发布,因为您在表单上没有必要的 enctype 属性.更改要使用的视图

                  Your file is not being posted because you do not have the necessary enctype attribute on the form. Change the view to use

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

                  您现在将获取文件并保存它,但与您的 Testimonials 对象没有关系,因此您无法检索它.您需要在 Testimonials 表中添加其他字段来存储文件属性(如果 Testimonials 可以有多个图像,则需要一个单独的表).我还建议您使用唯一标识符将文件保存到您的服务器(例如,一个 Guid 以防止在 2 个用户上传同名文件时意外覆盖).您修改后的模型可能是

                  You will now get the file and save it, but there is no relationship to your Testimonials object so you cannot retrieve it. You will need to add additional fields in your Testimonials table to store the file properties (or a separate table if a Testimonials can have multiple images). I also recommend you save the file to your server with a unique identifier (e.g. a Guid to prevent accidental overwriting if 2 users upload files with the same name). You revised model might be

                  public class Testimonials
                  {
                      public int Id { get; set; }
                      public string Testimonial { get; set; }
                      public string ImagePath { get; set; }
                      public string ImageDisplayName { get; set; }
                  }
                  

                  我还建议对包含上述属性的视图使用视图模型以及 public HttpPostedFileBase Image { get;放;} 以便您可以强绑定到模型并添加验证属性(例如 [FileSize] 属性,假设您不希望允许用户上传 2GB 文件).您的控制器方法将是

                  I would also recommend using a view model for the view that includes the above properties plus public HttpPostedFileBase Image { get; set; } so that you can strongly bind to the model and add validation attributes (for example a [FileSize] attribute assuming you do not want to allow users to upload 2GB files). Your controller method would then be

                  [HttpPost]
                  public ActionResult Create(TestimonialVM model)
                  {
                      // ModelState.IsValid check omitted
                      Testimonials testimonials = new Testimonials();
                      // map view model properties to the data model
                      ....
                      if (model.Image != null && model.Image.ContentLength > 0)
                      {
                          string displayName = model.Image.FileName;
                          string fileExtension = Path.GetExtension(displayName);
                          string fileName = string.Format("{0}.{1}", Guid.NewGuid(), fileExtension)
                          string path = Path.Combine(Server.MapPath("~/Images/"), fileName)
                          model.Image.SaveAs(path);
                          // Update data model
                          testimonials.ImagePath = path;
                          testimonials.ImageDisplayName = displayName;
                      }
                      TestimonialsContext testContext = new TestimonialsContext();
                      testContext.testimonialContext.Add(testimonials);
                      testContext.SaveChanges();
                      return RedirectToAction("Index");
                  }
                  

                  这篇关于ASP.NET MVC 上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法将大文件上传到 Google 文档 下一篇:确定 MAC 上传流文件的 MIME 类型

                  相关文章

                  最新文章

                • <i id='IX9q1'><tr id='IX9q1'><dt id='IX9q1'><q id='IX9q1'><span id='IX9q1'><b id='IX9q1'><form id='IX9q1'><ins id='IX9q1'></ins><ul id='IX9q1'></ul><sub id='IX9q1'></sub></form><legend id='IX9q1'></legend><bdo id='IX9q1'><pre id='IX9q1'><center id='IX9q1'></center></pre></bdo></b><th id='IX9q1'></th></span></q></dt></tr></i><div id='IX9q1'><tfoot id='IX9q1'></tfoot><dl id='IX9q1'><fieldset id='IX9q1'></fieldset></dl></div>

                  1. <tfoot id='IX9q1'></tfoot>

                      • <bdo id='IX9q1'></bdo><ul id='IX9q1'></ul>

                      <small id='IX9q1'></small><noframes id='IX9q1'>

                      <legend id='IX9q1'><style id='IX9q1'><dir id='IX9q1'><q id='IX9q1'></q></dir></style></legend>