Photo saved in Directory does not appear in Solution Explorer

Asked

Viewed 179 times

2

After a lot of pain, be able to save the photo in the directory and not in the bank. However it does appear on my tablet there in my Solution Explorer, more when opening the photo by windows explore it appears.

Because of this the photo is not displayed when editing the record. How to proceed?

Code that saves the Photo:

int n = 0;
        Boolean valido = true;
        foreach (var item in ModelState.Values)
        {
            n++;
            if ((item.Value == null) && (n < 12))
            {
                valido = false;
            }
        }
        if (valido == true)
        {
            var file = Request.Files[0];
            var fileName = Path.GetFileName(file.FileName);
            var pat = Path.Combine(Server.MapPath("~/Fotos/"), fileName);
            file.SaveAs(pat);

            frota.URLFoto = fileName;
            db.Frotas.Add(frota);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

Razor HTML:

@{ var foto = Model.URLFoto;
   var pat = Path.Combine("~/Fotos/", foto); }

<img src="@pat" width="172" height="92" style="border: 1px solid black; width: 172px; height: 92px;" />

2 answers

2


It is both normal and great that the photo does not appear in Solution Explorer, because the files that appear in Solution Explorer are files from the project. Incorrect would be if photos that do not belong to the project appeared in the project.

Now, if the photo does not appear when editing the record, it means that the procedure of uploading this photo is incorrect. Edit your question by placing the image upload code so I can provide a more targeted and correct answer to your problem.


EDIT

Missed Server.MapPath also when uploading the photo:

@{ var foto = Model.URLFoto;
   var pat = Path.Combine(Server.MapPath("~/Fotos/"), foto); }

<img src="@pat" width="172" height="92" style="border: 1px solid black; width: 172px; height: 92px;" />
  • Nothing yet! Photo does not appear

  • @Luizfelipe Please enter the HTML when rendering. What is populated in src?

  • I’ve already done it. it brings the full path of the image. and is a valid way!

  • @Luizfelipe You can update your question with what appears in HTML?

  • Ah, never mind. I’ll save the photos in the bank anyway!

  • 1

    I didn’t understand this line. Programming is this, it needs patience, determination and logic. Insist more, give more detail, edit the question with more richness of detail. I’m sorry, I’m happy to say, if you always give up in the face of the first difficulty, my friend, you won’t get far. I wish you success.

Show 1 more comment

1

To save an image to the database, first set the table that will receive the data, I suggest a table only with the image (which will be more practical when not loading the image), and one or more fields to indicate who it belongs to.

The field that will receive the image should be byte type for MSSQL, I do not know what your BD so you will have to search a little, in the place to load the photo, transform into bytes and send to the database, saving with the complementary data.

Transforming file into byte

byte[] arquivoByte = new byte[FileUpload.PostedFile.InputStream.Length+1];
FileUpload.PostedFile.InputStream.Read(arquivoByte,0,arquivoByte.Length);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.