0

I am trying to save picture in folder and store path in database using entity framework in asp.net mvc 5. I did it but I have some problems .

image path in DB saved like this :

C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods\2.jpg

How can I change it to: ~/Images/SubGoods/2.jpg ??

and I want to change image name to it's primary key id and I used pic =Convert.ToString( subgood.SubGoodID); to do it, but it saves Zero :

C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods\0

It saves always 0 .I know that's because Primary key in that line not generated yet . where of my codes primary Key id generated ?

public ActionResult AddSubGood(SubGood subgood, HttpPostedFileBase UploadImage)
    {
        var MainGoodId = subgood.FKMainGoodID;
        SubGoodRepositories blSubGood = new SubGoodRepositories();
        string path="";
        if (UploadImage != null)
        {
            string pic = System.IO.Path.GetFileName(UploadImage.FileName);
            pic =Convert.ToString( subgood.SubGoodID);
             path = System.IO.Path.Combine(
                                   Server.MapPath("~/Images/SubGoods"), pic);

        }

        if (ModelState.IsValid)
        {
            subgood.FKMainGoodID = MainGoodId;
            UploadImage.SaveAs(path);
            subgood.SubGoodImage = path;

            if (blSubGood.Add(subgood))
            {
                return JavaScript("alert('saved');");
            }
            else
            {
                return JavaScript("alert('didn't saved');");
            }
        }
        else
        {
            return JavaScript("alert('error');");
        }

    }
shm
  • 439
  • 2
  • 11
  • 25
  • you can add "/Images/SubGoods" to web.config and save only the filename in the database. Concat them when you want to access the file. – adiga Jan 05 '16 at 07:03
  • Not sure about saving the file with PK. Because you save the file physically first and then save the URL in the DB. Saving the file with timestamp is the best option imo. – adiga Jan 05 '16 at 07:06
  • Thank you , It's mean there is no way to do it using `Server.MapMath` ? @adiga – shm Jan 05 '16 at 07:07
  • with timestamp ?! OK , Thanks @adiga – shm Jan 05 '16 at 07:09

2 Answers2

1

Server.MapPath will return you Virtual path(which you don't needed) , you can create another variable and concatenate like this :

string DbPath = "~/Images/SubGoods/"; // better to store in web.config file
DbPath = DbPath  + ""//here you can query in table and find the last inserted primary key and increment it with '1'
Hardik
  • 228
  • 1
  • 6
1

You should save only the file name:

var fileName = Path.GetFileName(UploadImage.FileName);

Then when you want to fetch the file for user you can simply address the file name with specific path:

<img src="~/Content/Uploaded/@item.fileName" .../>

You can also generate a random file name using Guid:

var rondom = Guid.NewGuid() + fileName;
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110