Solved

Uploading images

Posted on 2008-10-06
8
269 Views
Last Modified: 2013-12-25
I am a beginner and this is my very first website project.

I am populating an array using Server.MapPath. On my local machine it is trying to read from
C:\Documents and Settings\\My Documents\Visual Studio 2008\Projects folder. I thought the Server.MapPath would read from C:\inetpub\wwwroot.
The array I am creating is getting all images from a folder. I want to allow the user to upload images to this folder. When I save the images from the user the images are being stored in C:\inetpub\wwwroot on my local machine.

here is my save path
        string savePath = @"c:\inetpub\wwwroot\SlideShow\Files\" + folderName;

this is how I retrieve the images
    protected void Page_Load(object sender, EventArgs e)
    {
        //create a string to hold the images for the banner
        StringBuilder imageArray = new StringBuilder();
        imageArray = GetImageArray(ResolveClientUrl(folderName));
        //register array so it can be referenced in javascript
        ClientScriptManager csm = Page.ClientScript;
        csm.RegisterArrayDeclaration("Pictures", imageArray.ToString());

    protected StringBuilder GetImageArray(string folderName)
    {
        string Path;
        DirectoryInfo Folder;

        FileInfo[] Images;
        StringBuilder ImageArray = new StringBuilder();

        Path = Server.MapPath(folderName);
        Folder = new DirectoryInfo(Path);
        Images = Folder.GetFiles();
               
        // Looping through all files in the folder and generating
        // a JavaScript array declaration.
        foreach (FileInfo Image in Images)
        {
            if (Image.Extension.StartsWith(".jpg") || Image.Extension.StartsWith(".JPG"))
                ImageArray.AppendFormat("'{0}/{1}',", folderName, Image.Name);
            else
                continue;
        }

        if (ImageArray.Length > 0)
        {
            // Removing the unwanted last comma.
            ImageArray.Remove(ImageArray.Length - 1, 1);
        }

        return ImageArray;
    }
0
Comment
Question by:cramer0240
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 300 total points
ID: 22654846
Server.MapPath will map to the projects folder when running in debug because this is where it is executing, however when deployed to the iis folder it shoud map correctly...
0
 

Author Comment

by:cramer0240
ID: 22654870
Would I be better off saving the images with something similar to server.mappath rather than writing out  @"c:\inetpub\wwwroot\SlideShow\Files\"?
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22654993
Yes, you should never hard code paths, if you need to use the full path like that you can use the web.config file to store configuration...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22660395
use
Path = Server.MapPath(folderName);

the best way to code is using \ and ..\ instead of hardcoding the C:\......

http://www.asp101.com/resources/paths.asp

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22661116
>>>On my local machine it is trying to read from
C:\Documents and Settings\\My Documents\Visual Studio 2008\Projects folder
Is this the folder that you have all your ASPX files in?
>>>string savePath = @"c:\inetpub\wwwroot\SlideShow\Files\" + folderName;
Is "SlideShow" the virtual directory of your application?

 
0
 
LVL 7

Assisted Solution

by:ASPSQLServerCOM
ASPSQLServerCOM earned 200 total points
ID: 22661215
one quick thing, are you running this code in Visual Studio debug, try to deploy your code under c:\inetpub\wwwroot\SlideShow\............. and then check
0
 

Author Comment

by:cramer0240
ID: 22662919
I did deploy it and checked it and it worked. I was just kinda fuzzy on why Server.MapPath was reading files from the visualstudio/projects folder on my local machine when Server.MapPath was storing images in c:\inetpub\wwwroot\SlideShow\.............  on my local machine. But it is working on the server. I am learning new things and discovering new things each day I work on this. Thanks guys..gals?.....
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22663021
FYI, guys and gals have different icons. :-)

2008-10-07-144959.jpg
2008-10-07-144930.jpg
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 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