【问题标题】:Create Folder depends on ID , upload images , and save the path in database in asp.net MVC创建文件夹依赖ID,上传图片,并将路径保存在asp.net MVC中的数据库中
【发布时间】:2018-08-28 22:40:23
【问题描述】:

我在 Property Project 工作,允许系统用户为它添加属性上传图像。我需要做什么创建文件夹取决于属性的ID,将路径保存在数据库中并将图像上传到此文件夹中。

我在我的属性模型中创建:

public string Images { get; set; }

在我创建的视图中:

                <div class="row">
                    <div class="col-md-2"></div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label"> choose image   </label>
                            <input multiple type="file" title="choose image" id="files" name="PropImage" onchange="show(this)" />
                        </div>
                    </div>
                    <div class="col-md-4"></div>
                </div>

我的 JavaScript :

 <script type="text/javascript">
        function handleFileSelect() {
            //Check File API support
            if (window.File && window.FileList && window.FileReader) {

                var files = event.target.files; //FileList object
                var output = document.getElementById("result");

                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    //Only pics
                    if (!file.type.match('image')) continue;

                    var picReader = new FileReader();
                    picReader.addEventListener("load", function (event) {
                        var picFile = event.target;
                        var div = document.createElement("div");
                        div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" + "title='" + picFile.name + "'/>";
                        output.insertBefore(div, null);
                    });
                    //Read the image
                    picReader.readAsDataURL(file);
                }
            } else {
                console.log("Your browser does not support File API");
            }
        }

        document.getElementById('files').addEventListener('change', handleFileSelect, false);

现在在我的控制器中我无法创建文件夹,因为尚未创建属性 ID,我该怎么办?我应该有权创建文件夹吗?

我的控制器:

public ActionResult CreateProperties(AddPropertyViewModel 模型, 列出道具图像) {

        if (ModelState.IsValid)
        {

            foreach (var item in PropImage)
            {
                var path = Path.Combine(Server.MapPath(@"~\ImgUp\" /*+ PropImagefolderName*/), item.FileName);//+ IdentityImageFolderName)
                item.SaveAs(path);

            }

            //model.PropertiesVM.ID = properToAdd.PropAddress.Id;
            // string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension  
            // string myfile = name + "_" + properToAdd.ID + ext; //appending the name with id  
            // store the file inside ~/project folder(Img) 
            var imagepath = Server.MapPath(PropImageDirctory);
            //var IdentityPath = Server.MapPath(IdentityImageDirctory);
            properToAdd.Images = imagepath;
            properToAdd.Build_area = model.PropertiesVM.Build_area;
            properToAdd.Earth_area = model.PropertiesVM.Earth_area;
            properToAdd.AddedDate = DateTime.Now;
            properToAdd.Prop_Type_ID = Prop_type_ID;
            properToAdd.Plate_ID = plateID;
            properToAdd.Branch_ID = BranshID;
            properToAdd.Price = model.PropertiesVM.Price;
            properToAdd.AddedBy = FullName; /*currentUserName;*/

            db.D_Properties.Add(properToAdd);

            //IdentityImage.SaveAs(Path.Combine(IdentityFolderPath, IdentityImageFileName));
            // InstrumentImage.SaveAs(Path.Combine(IdentityPath, InstrumentImageFileName));
            db.SaveChanges();
            TypesDropDownList();
            PlatesDropDownList();
            BranchesDropDownList();
            TempData["noti"] = "Success";
            return RedirectToAction("CreateProperties");
        }

            //ViewBag.message = "Please choose only Image file";
            // If we got this far, something failed, redisplay form  
            TypesDropDownList();
            PlatesDropDownList();
            BranchesDropDownList();
            TempData["noti"] = "Error";
        return View();

    } 

【问题讨论】:

  • 属性id是谁生成的?数据库是我的猜测。您需要将其写入数据库以检索 id - 这不是问题,因为您不需要进一步的信息来“创建”该属性。检索生成的 id 可以在与数据库插入相同的操作中完成 - 无论您是否使用实体框架。请在您的帖子中添加更多详细信息。
  • 是 id 从数据库生成。你的意思是在保存更改方法后我可以获得当前的 id 吗?
  • 是的,你可以。在不检索 id 的情况下编写您的控制器操作,编辑您的帖子并添加控制器的定义,然后我们将向您展示如何检索 id。
  • 我更新了我的问题

标签: c# asp.net file model-view-controller file-upload


【解决方案1】:

如果您的Property 类(或properToAdd 的任何类型)具有正确定义的id 属性,则一旦您调用db.SaveChanges();,对象properToAdd 的ID 将包含新的数据库记录ID - 问题解决了。

【讨论】:

    【解决方案2】:

    如果您要在保存属性详细信息之前通过ajax 或其他方式上传图像,这意味着您仍然没有为新记录分配任何 ID,那么在我看来,您可以执行以下操作。

    1. 您在服务器上有tempImgs 文件夹。将文件重命名为 $_SESSION['userid']+i
    2. 提交房产记录时
    3. 获取新提交的记录 ID。
    4. 创建像'../uploads/'+&lt;&lt;new record id&gt;&gt;这样的目录
    5. 将文件从tempImgs 移动到新创建的文件夹中

    希望这能解决您的问题

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多