Solved

Server.MapPath and new folders

Posted on 2009-04-07
8
486 Views
Last Modified: 2012-05-06
I'm using Server.Mappath to return some images and display them in a datalist. The file that stores the images was going to just be the Images folder in my solition. Now before I create and store the images, I'm creating a new folder under the images folder. Everything works except when it goes to display the images it no longer knows where to look. Any idea how to fix this?

dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/"));
dlImages.DataBind();

The new folder is created in the code preceding this, so there is a variable I can work with. How can I get it to look for the newly created folder, which will change all of the time based on what was just created?
This did not work:
dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/" + newFolder));
Any help is appreciated.
0
Comment
Question by:dodgerfan
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24088822
Can you show how "newFolder" was created? Seems like you have a problem with the slashes of paths. If newFolder is a whole path, or if it starts out with a slash, you need a different approach.
0
 

Author Comment

by:dodgerfan
ID: 24089205
I've attached my complete function in a code snippet. I'm using Aspose slides for some of this.
Label2.text = "File Uploaded: " + hiddenValue;

string newPath = System.IO.Path.GetFileNameWithoutExtension(hiddenValue.Value);

string thumbnailPath = System.ConfigurationManager.AppSettings["uploads"];
 

Directory.CreateDirectory(thumbnailPath + newPath);

string FinalPath = thumbnailPath + newPath + "/";
 

Presentation srcPres = new Presentation(uploadPath + hiddenValue.Value);

int lastSlidePostion = srcPres.Slides.LastSlidePosition;

for (int i=1; i<= lastSlidePosition; i++)

{

Slide sld = srcPres.GetSlideByPosition(i);

double scaleX = 1.3;

double scaleY = 1.3;

System.Drawing.Image img = sld.GetThumbnail(scaleX, scaleY);

img.Save(String.Concat(finalPath, "Slide_", i.ToString().PadLeft(2, '0'), ".jpeg"), ImageFormat.Jpeg);

}

dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/"));

dlImages.DataBind();

Open in new window

0
 

Expert Comment

by:aneelmehta
ID: 24095680
you are creating new folder in "thumbnailPath" and trying to access it in "images" folder are they both pointing to same location ? If yes then no need to use "Server.MapPath" as u have same path in string FinalPath = thumbnailPath + newPath + "/";
0
 

Author Comment

by:dodgerfan
ID: 24096622
I've tried using finalPath like this:
dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath(finalPath);
It does not work. My datalist on the html page is below. The images/ is wrong now, correct? How do I fix that?

<asp:datalist id="dlImage" runat="server" RepeatDirection="Vertical">
  <ItemTemplate>
    <a href='<%= ResloveUrl("~/")images/<%# GetFileName(Container.DataItem.ToString()) %>' target="main">  
        <img src='<%= ResloveUrl("~/")images/<%# GetFileName(Container.DataItem.ToString()) %>' alt="Image" height="150" width="150" border="0" />
    </a>
  </ItemTemplate>
</asp:DataList>
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24097424
> Directory.CreateDirectory(thumbnailPath + newPath);

this is a dangerous line. Suppose thumbnailPath were "c:\temp" and newPath were "c:\my documents" then CreateDirectory will fail. Suppose one of them is relative, i.e., thumbnailPath is "c:\temp" and newPath is "somePath\OtherPath", then the CreateDirectory will also fail. I don't know what the variables contain, but you should try to make them correct (no leading slash or drive specs on newPath, always a trailing slash on thumbnailPath) before you combine them.

You seem to do the same with uploadPath, by doing new Presentation(uploadPath + hiddenValue.Value). The varaible uploadPath is not defined in the code above, so I cannot say much sensible about it, but again it can be slightly tricky to get right.

Later (where you use String.Concat instead of "+" to combine strings), you use finalPath. Which may not even exist if CreateDirectory had failed in the first place (but that would raise an error).

Finally, about your actual question, you look into a total different location than you have been storing the thumbnailes. (i.e., now you use Server.MapPath, which will look relative to your directory, which is by no means the same as the hiddenValue and thumbnailPath combined. If you intend to read back what you just saved, you should use something like:

dlImages.DataSource = System.IO.Directory.GetFiles(finalPath);

instead.
0
 

Author Comment

by:dodgerfan
ID: 24097608
abel, thanks for the insight and help. A lot of the code at this point is a work in progress as I try different things to get it working again. I have tried your suggestion before about the dlImages.DataSoure = System.IO.Directory.GetFiles(finalPath);
It does not work for some reson. Is there something else I should try?
0
 

Author Closing Comment

by:dodgerfan
ID: 31567589
Go it. Had to remove ResloveUrl from the html page calling the datalist. thanks.
0
 
LVL 39

Expert Comment

by:abel
ID: 24105262
> Go it. Had to remove ResloveUrl from the html page calling the datalist. thanks.

Good, glad you got it working in the end
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 66
Is IHttpActionResult a promise pattern? 2 37
Library to convert HTML to PDF. 8 46
Image(2) 3 28
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now