Anabeil
Anabeil

Reputation: 73

Create Folder depends on ID , upload images , and save the path in database in asp.net MVC

i am work in Property Project that allow system user when add Property upload Images for it . what i need to doing create folder depends on ID of property ,save the path in database and uploaded the images in this folder .

i create in my Property model :

public string Images { get; set; }

in View i create :

                <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>

my 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);

now in my controller i cant create folder because the ID of property not created yet , how i can do ? and should i have permissions to create folder ?

my controller :

public ActionResult CreateProperties(AddPropertyViewModel model, List PropImage) {

        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();

    } 

Upvotes: 0

Views: 1297

Answers (2)

Niaz Ur Rehman
Niaz Ur Rehman

Reputation: 9

if you are going to upload images by ajax or whatever before saving properties details it means still you are not assigning any id for new records then in my view you can do as following.

  1. you have tempImgs folder on server. rename file to $_SESSION['userid']+i
  2. when submiting property record
  3. get the new submitted record ID.
  4. create directory like '../uploads/'+<<new record id>>
  5. move file from tempImgs to the new created folder

hope this answers to your concern

Upvotes: 1

Mihai Ovidiu Drăgoi
Mihai Ovidiu Drăgoi

Reputation: 1317

If your Property class (or whatever type properToAdd is) has an id property that is properly defined, once you call db.SaveChanges(); the object properToAdd's id will contain the new database record id - problem solved.

Upvotes: 1

Related Questions