store files at my web host - how

Discussion in 'ASP .Net' started by Tim Zych, Jun 5, 2004.

  1. Tim Zych

    Tim Zych Guest

    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.
     
    Tim Zych, Jun 5, 2004
    #1
    1. Advertising

  2. Tim Zych

    John Guest

    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" <tzych@noworms_earth_link.dott.net> wrote in message
    news:u%...
    > 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.
     
    John, Jun 6, 2004
    #2
    1. Advertising

  3. Tim Zych

    Tim Zych Guest

    Thanks John.

    "John" <> wrote in message
    news:p2xwc.8110$...
    > 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" <tzych@noworms_earth_link.dott.net> wrote in message
    > news:u%...
    > > 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.

    >
    >
     
    Tim Zych, Jun 6, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    671
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. Orpheus66
    Replies:
    0
    Views:
    4,527
    Orpheus66
    Jul 30, 2003
  3. Moiristo
    Replies:
    0
    Views:
    396
    Moiristo
    Jul 24, 2006
  4. unomystEz
    Replies:
    3
    Views:
    339
    unomystEz
    Oct 29, 2006
  5. Jerry Qu
    Replies:
    1
    Views:
    528
    lee atkinson
    Feb 20, 2009
Loading...

Share This Page