0

I use File Uploader control. I want the user to:

1) Search any location

2) Choose and load any .JPG file or any other image format file

3) Store that particular file in the DB (SQL).

Solution: I achieved all these, however I hardcoded the path.

Problem: I don't know how to remove this hard coding and achieve above 3 points :( Please help tweak the code:

if (FileUpload1.HasFile)
{
   FileStream FS = new FileStream(@"C:\Users\Ramakrishnan\Desktop\New folder\001.jpg", FileMode.Open, FileAccess.Read);
   byte[] img = new byte[FS.Length];
   FS.Read(img, 0, Convert.ToInt32(FS.Length));
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • You want a user to search his computer using your webcontrol? Or do you mean select a file to upload from his directory? – Kristof Aug 24 '12 at 14:53
  • Are you trying to take the contents from the `FileUpload1` and store them in the database? – CodingGorilla Aug 24 '12 at 14:53
  • @Kristof: Select a file to upload from his directory he chooses. I mean ideally it should replace the @"C:\Users\Ramakrishnan\Desktop\New folder\001.jpg" in my code above dynamically and use the file name he chooses rather than this hardcoded filename 001.jpg..... So only that first parameter I wanna make it dynamic. – Jasmine Aug 24 '12 at 15:00
  • @CodingGorilla: Yeup contents as in the file name I think, I am converting to stream of bytes for storing in DB. But I could achieve all those. I just want to tweak this code to make the first parameter dynamic rather than hardcoding like this. As because the user may search another directory or even have many image files in this directory folder but its his liberty to choose whichever file he want. I wanna achieve that :( – Jasmine Aug 24 '12 at 15:02
  • 1
    @Divine The file upload control has a stream property, just substitute your `FS` vaiable for `FileUpload1.FileContent`. – CodingGorilla Aug 24 '12 at 15:06

4 Answers4

0

FileUpload1.FileName

How to: Upload Files with the FileUpload Web Server Control

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

These links explain it with examples for winforms:

http://dotnetperls.com/openfiledialog

http://www.geekpedia.com/tutorial67_Using-OpenFileDialog-to-open-files.html

This one for webforms:

FileUpload to FileStream

Community
  • 1
  • 1
Divi
  • 7,621
  • 13
  • 47
  • 63
0
        string filename = Path.GetFileName(FileUpload1.FileName);
        FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read);
GrayFox374
  • 1,742
  • 9
  • 13
0

The file upload control has a stream property, just substitute your FS vaiable for FileUpload1.FileContent.

** UPDATE **

Based on the discussion in the comments I think you're still doing it wrong.

Your code should be:

if (FileUpload1.HasFile)
{
   byte[] img = new byte[FileUpload1.FileContents.Length];
   FileUpload1.FileContents.Read(img, 0, Convert.ToInt32(FS.Length));
   // Store the image in the DB
}

There's no reason to try to open a file from the file system, the file you are interested in does not exist on the web server's file system.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • I am extremely sorry, I see that its always taking the first file :( Not the one I choose. But no exceptions. byte[] img = new byte[FileUpload1.FileContent.Length]; FileUpload1.FileContent.Read(img, 0, Convert.ToInt32(FileUpload1.FileContent.Length)); – Jasmine Aug 24 '12 at 16:08
  • Do you have more than one file upload control? – CodingGorilla Aug 24 '12 at 16:14
  • No sir :( Only one file upload control. I dont know why, it always takes the first file name I also tried this string file = Path.GetFullPath(FileUpload1.PostedFile.FileName); FileStream FS = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] img = new byte[FS.Length]; FS.Read(img, 0, Convert.ToInt32(FS.Length)); – Jasmine Aug 24 '12 at 16:23
  • I don't know what you mean by the "First file name" what are you expecting? – CodingGorilla Aug 24 '12 at 16:30
  • Well, I mean to say, out of many files in that folder, it selects the first file. I reckon it as first file because I see the same image stored in DB always for any insert :( – Jasmine Aug 24 '12 at 16:35
  • You've totally lost me, you're not reading a file from the file system, you're taking a file that was uploaded by a web browser to an ASP.NET page, right? – CodingGorilla Aug 24 '12 at 16:37
  • Hi, yeup sir, I am doing the same. Well surprisingly this looks to be working now. string file = Path.GetFullPath(FileUpload1.PostedFile.FileName); FileStream FS = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] img = new byte[FS.Length]; FS.Read(img, 0, Convert.ToInt32(FS.Length)); FS.Close(); – Jasmine Aug 24 '12 at 16:42
  • I am very sorry for confusing you, I am newbie for all these :( – Jasmine Aug 24 '12 at 16:42
  • Noo Its a serious problem now. It is not working. I deleted all my data in DB and then it inputs new picture. However its not working :( It again and again inputs same picture :( – Jasmine Aug 24 '12 at 16:47
  • Hi, thank you, I am afraid now. Still its not behaving the way we want :( :( – Jasmine Aug 24 '12 at 16:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15770/discussion-between-coding-gorilla-and-divine) – CodingGorilla Aug 24 '12 at 17:00
  • Your guidance helped me a lot to achieve what I want, thank you for helping me. – Jasmine Aug 24 '12 at 18:22