By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

store files at my web host - how

P: n/a
How would I go about storing data files on my web host?

I have an Access database driven website and I would like to save files and
be able to download them. Can somebody point me in the right direction of
how I would go about doing that?

I am thinking about storing the locations of them in the database and the
physical files somewhere else. Not sure how to go about it though.

Thanks.


Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bit of a newb, here, so take my advice with a grain of salt, but:

Have a look for some file uploading tutorials - most of them will show you
how to upload and then save files to disk. You can grab filenames either
from the file input field or from user input via a textbox or something. You
can save the file to disk with that name, and then pass the name and path to
a database.

I'm doing something similar with product images, so I'll post a little of my
code - keep in mind it's probably not entirely best practice, and likely
poorly coded, but hopefully it'll start you in the right direction! ;)

============================

private void btnUpload_Click(object sender, System.EventArgs e)
{
if(fileImage.PostedFile != null &&
(fileImage.PostedFile.ContentType == "image/jpeg" ||
fileImage.PostedFile.ContentType == "image/pjpeg" ||
fileImage.PostedFile.ContentType == "image/gif" ||
fileImage.PostedFile.ContentType == "image/png"))
{

string fileName = fileImage.PostedFile.FileName;
char[] dirChars = new char[2] {'/','\\'};
fileName = fileName.Remove(0,fileName.LastIndexOfAny(dirChars ) + 1);

fileImage.PostedFile.SaveAs(Server.MapPath("..\\pr dimages") + "\\" +
fileName);

PopulatePage();
}

=====================

For this code I have a file input field turned into a server control named
'fileImage'. Next to it I have a button (not the 'browse' button) called
btnUpload that runs this method. I check to make sure a file has been posted
and it's a certain contenttype. Then I try to get the filename alone (maybe
not the best way to do it) and save it in a directory on the webserver. This
keeps the filename the same - it's probably better to let the user specify a
new one.

If you want, you could add code to take the filename you got here and add it
to a database. You can grab a bit of metadata as well from the PostedFile,
and store that too if necessary. I've done that in another method, to
associate images with products. So you could always grab your image paths
from the database. An alternative method might be, however, to do something
like this:

========

public string[] prodGetImageNames()
{
string path = HttpContext.Current.Server.MapPath("..\\prdimages" );
string[] filesTemp = Directory.GetFiles(path);
string[] fileNames = new string[filesTemp.Length];

for(int i=0; i<filesTemp.Length; i++)
{
fileNames[i] = Path.GetFileName(filesTemp[i]); //changes fullpath to
filename
}

return fileNames;
}

=========

This method looks into my products images directory, grabs all the
filenames, and uses them to populate a string array that I return as a
datasource to a dropdown list. It uses System.IO. I can guess at about 20
ways to code this better, but am not skilled enough to do it yet; but
hopefully it gives you an idea for a possibility. I don't know if it's
faster to grab the names from the database or from the filesystem, though..
probably DB?

Anyway, good luck!

-John

"Tim Zych" <tzych@noworms_earth_link.dott.net> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
How would I go about storing data files on my web host?

I have an Access database driven website and I would like to save files and be able to download them. Can somebody point me in the right direction of
how I would go about doing that?

I am thinking about storing the locations of them in the database and the
physical files somewhere else. Not sure how to go about it though.

Thanks.

Nov 18 '05 #2

P: n/a
Thanks John.

"John" <jc*****************@hotmail.com> wrote in message
news:p2*****************@news-server.bigpond.net.au...
Bit of a newb, here, so take my advice with a grain of salt, but:

Have a look for some file uploading tutorials - most of them will show you
how to upload and then save files to disk. You can grab filenames either
from the file input field or from user input via a textbox or something. You can save the file to disk with that name, and then pass the name and path to a database.

I'm doing something similar with product images, so I'll post a little of my code - keep in mind it's probably not entirely best practice, and likely
poorly coded, but hopefully it'll start you in the right direction! ;)

============================

private void btnUpload_Click(object sender, System.EventArgs e)
{
if(fileImage.PostedFile != null &&
(fileImage.PostedFile.ContentType == "image/jpeg" ||
fileImage.PostedFile.ContentType == "image/pjpeg" ||
fileImage.PostedFile.ContentType == "image/gif" ||
fileImage.PostedFile.ContentType == "image/png"))
{

string fileName = fileImage.PostedFile.FileName;
char[] dirChars = new char[2] {'/','\\'};
fileName = fileName.Remove(0,fileName.LastIndexOfAny(dirChars ) + 1);
fileImage.PostedFile.SaveAs(Server.MapPath("..\\pr dimages") + "\\" + fileName);

PopulatePage();
}

=====================

For this code I have a file input field turned into a server control named
'fileImage'. Next to it I have a button (not the 'browse' button) called
btnUpload that runs this method. I check to make sure a file has been posted and it's a certain contenttype. Then I try to get the filename alone (maybe not the best way to do it) and save it in a directory on the webserver. This keeps the filename the same - it's probably better to let the user specify a new one.

If you want, you could add code to take the filename you got here and add it to a database. You can grab a bit of metadata as well from the PostedFile,
and store that too if necessary. I've done that in another method, to
associate images with products. So you could always grab your image paths
from the database. An alternative method might be, however, to do something like this:

========

public string[] prodGetImageNames()
{
string path = HttpContext.Current.Server.MapPath("..\\prdimages" );
string[] filesTemp = Directory.GetFiles(path);
string[] fileNames = new string[filesTemp.Length];

for(int i=0; i<filesTemp.Length; i++)
{
fileNames[i] = Path.GetFileName(filesTemp[i]); //changes fullpath to filename
}

return fileNames;
}

=========

This method looks into my products images directory, grabs all the
filenames, and uses them to populate a string array that I return as a
datasource to a dropdown list. It uses System.IO. I can guess at about 20
ways to code this better, but am not skilled enough to do it yet; but
hopefully it gives you an idea for a possibility. I don't know if it's
faster to grab the names from the database or from the filesystem, though.. probably DB?

Anyway, good luck!

-John

"Tim Zych" <tzych@noworms_earth_link.dott.net> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
How would I go about storing data files on my web host?

I have an Access database driven website and I would like to save files

and
be able to download them. Can somebody point me in the right direction of how I would go about doing that?

I am thinking about storing the locations of them in the database and the physical files somewhere else. Not sure how to go about it though.

Thanks.


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.