store files at my web host - how

T

Tim Zych

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.
 
J

John

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("..\\prdimages") + "\\" +
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 = Path.GetFileName(filesTemp); //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
 
T

Tim Zych

Thanks John.

John said:
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("..\\prdimages") + "\\" +
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 = Path.GetFileName(filesTemp); //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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top