all groups > visual studio .net general > june 2005 >
You're in the

visual studio .net general

group:

uploading images to a hosted server


uploading images to a hosted server patrick_a
6/26/2005 10:06:01 PM
visual studio .net general: 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";
}
RE: uploading images to a hosted server AGupta
6/30/2005 4:51:02 AM
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

[quoted text, click to view]
AddThis Social Bookmark Button