1

I'm trying to display images I've loaded previously on a view but getting no success. Images are uploaded, that's for sure. So far I've written something like this:

@if(Model.AttachedInformation.Count > 0)
{
    <div id="gallery">
        @foreach(var path in Model.AttachedInformation)
        {
            <img src="@path" alt="default_description" title="some_title" />
        }
    </div>
}

AttachedInformation is simply a ICollection<String> object.

But that's giving me only borders of images. Moreover I checked @path variable and it's really keeping full file paths.

Suggestions are appreciated! Thanks!

EDIT: CONTROLLER

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Employee employee, IEnumerable<HttpPostedFileBase> files)
{
    if(ModelState.IsValid)
    {
        var filepath = String.Empty;

        foreach(var file in files)
        {
            if(null != file && file.ContentLength > 0)
            {
                filepath = Path.Combine(
                    HttpContext.Server.MapPath("~/Content/Images/Uploads"),
                    Path.GetFileName(file.FileName)
                );
                file.SaveAs(filepath);
                employee.AttachedInformation.Add(filepath);
            }
        }                

        this.repository.Add(employee);

        return Redirect("/");
    }
    else
    {
        return View(employee);
    }
}

MODEL

AbstractEntity keeps Version and ID properties.

[Serializable]
public class Employee : AbstractEntity<Employee>, IAggregateRoot
{
    public Employee()
    {
        this.AttachedInformation = new HashSet<String>();
    }

    public virtual String FirstName { get; set; }
    public virtual String MiddleName { get; set; }
    public virtual String LastName { get; set; }
    public virtual String SSN { get; set; }
    public virtual String EmployeeNumber { get; set; }
    public virtual String TradeUnionNumber { get; set; }
    public virtual String Department { get; set; }
    public virtual String Post { get; set; }
    public virtual DateTime DateOfHire { get; set; }
    public virtual DateTime DateOfBirth { get; set; }
    public virtual ICollection<String> AttachedInformation { get; set; }
}

VIEW

View contains several inputs with name files like:

<input type="file" name="files" id="additional" class=" " accept="image/jpeg,image/png,image/gif" />

RAW HTML

<div id="gallery">
    <img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Desert.jpg" alt="default_description" title="some_title" />
    <img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Hydrangeas.jpg" alt="default_description" title="some_title" />
    <img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Jellyfish.jpg" alt="default_description" title="some_title" />
    <img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Koala.jpg" alt="default_description" title="some_title" />
</div>

BOUNTY

What I need is to get an illustrated example (that works). Thanks!

4
  • 1
    "Really keeping full file paths" as in file:/// paths? Or do the paths actually work in the browser? Commented Mar 3, 2012 at 0:42
  • 2
    Please provide the HTML output for that function... (or maybe you get some error when you go to the page?) Commented Mar 3, 2012 at 0:44
  • Could you include a controller snipped showing how you populate the model? Commented Mar 3, 2012 at 0:45
  • Can you show the raw html that gets rendered? And, have you checked that the file is saving to the Upload directory correctly? Commented Mar 3, 2012 at 1:24

2 Answers 2

4
+50
HttpContext.Server.MapPath("~/Content/Images/Uploads")

The above is going to give you the full path on disk which you are then saving to the employee record. You should just be saving the name of the file and then sourcing the image to ~/Content/Images/Uploads/

One other note is that you are saving all of the images to the same directory by file name. If two users upload a file with the same name one will overwrite the other. Each employee should have their own upload directory or you should dynamically generate the file names.

Sign up to request clarification or add additional context in comments.

2 Comments

Agree! Especially about overwriting. So probably I'll add id as a part of the path so it's gonna be unique! Could you please illustrate yours ~/Content/Images/Uploads/?
In order to prevent tons of sub directories, we have also used GUID file names (as well as preserving the uploaded file name for display purposes). This way, we have unique stored file names, but can render the name that the user uploaded and avoid (mostly) the collision of file names when storing to disk. My 2 cents :)
1

Ok. I found a solution. Maybe it will be useful for somebody.

var directory = "/Content/Images/Uploads/" + employee.SSN;
var path = String.Empty;

if(!Directory.Exists(Server.MapPath(directory)))
{
    Directory.CreateDirectory(Server.MapPath(directory));
}

foreach(var file in files)
{
    if(null != file && file.ContentLength > 0)
    {
        path = Path.Combine(directory + "/", Path.GetFileName(file.FileName));
        file.SaveAs(Server.MapPath(path));
        employee.AttachedInformation.Add(path);
    }
}

That works for me. Corrections are appreciated.

3 Comments

SSNs are not unique... Also the their SSN will be in every proxy servers folders. Not safe. If you must use the SSN, then atleast hash it some how to a random string
Ok, thank you! See.. Use of Id is complicated because it gets generated when entity is saved to the repository.
No I mean calculate a one-way hash to convert the SSN to something else.. stackoverflow.com/questions/5264644/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.