Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / General / June 2005

Tip: Looking for answers? Try searching our database.

uploading images to a hosted server

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
patrick_a - 27 Jun 2005 06:06 GMT
Hello,
    I am deploying an ASP.net application written in C# to a hosting server
where I have limited privileges.  I know how to do image file uploads if I'm
doing them with a path involving C:\ but what path do I use when I don't know
that information.  Do I set up a folder relative to my application folder in
the www directory (ie. www/myapplication/uploads )and then save the image to
it with a relative path like uploads/myimage.jpg  or as an absolute path like
www.mysite.com/myapplication/uploads ? How can I authenticate a user before
they can upload to the folder - do I create a user on the server with the
upload folder as their root directory and then somehow pass the username and
password in a string to be authenticated?  Here is what the code looks like .
Thanks for any help you can give me.

protected System.Web.UI.HtmlControls.HtmlInputFile uploadPhoto;
protected System.IO.Stream stream1;

string strUploadDirectory = "uploads/";     //I need to know this directory
path.
string strUserName;
string strFileName;
string strPhotoTitle1;
string strFileAndDirectory;
long lMaxFileSize = 150000;
           
strUserName=User.Identity.Name;
strFileName=uploadPhoto.PostedFile.FileName.Trim();

strFileName=Path.GetFileName(strFileName);

if(strFileName!="")
{
    //test for file size
    if ((uploadPhoto.PostedFile.ContentLength <= lMaxFileSize) &&
(uploadPhoto.PostedFile.ContentLength > 0))

    {    //create a unique name for the file
        strFileAndDirectory=strUploadDirectory + strUserName + strFileName;
        FileInfo fileinfo1 = new FileInfo(strFileAndDirectory);

        if (fileinfo1.Exists)//if file exists delete it

        {    
            fileinfo1.Delete();
        }
        try
        {    // load the uploaded file into a stream
            stream1 = uploadPhoto.PostedFile.InputStream;

            // this generates a handled error if file is not an image
            image1 = System.Drawing.Image.FromStream(stream1);

            // save image to upload directory
            image1.Save(strUploadDirectory + strUserName + strFileName);
        }
        catch
        {
            strFileName="no_image.jpg";
                    }
        }
    else
    {
        strFileName="no_image.jpg";
    }

}
else
{
    strFileName="no_image.jpg";
}
           
AGupta - 30 Jun 2005 12:51 GMT
For uploading a file to the hosting server you can get the physical path of
your website's root folder by using Server.MapPath("<rootfoldername>")  and
then append the rest of the folder path to this physical root folder. So lets
say you wish to upload images to "Dataimages" folder under your website's
root then the physical path will be Server.MapPath("/") + "Dataimages"

For uploading you will not need to pass any user's authetication
information, since the file is uploaded by the ASP.net application. All you
would need to do is to get read/write permissions for the ASPNET system user.

- AGupta

> Hello,
>     I am deploying an ASP.net application written in C# to a hosting server
[quoted text clipped - 66 lines]
> }
>            

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.