tags:

views:

58

answers:

2

hi I use the code

    SqlConnection conn = new SqlConnection("Data Source=DDPRO8-WIN7X86\\SQLEXPRESS;Initial Catalog=mp3bytes;Persist Security Info=True;Integrated security=true; User ID=; Password=;");
    SqlCommand cmd = null;
    SqlParameter param = null;
    cmd = new SqlCommand(" INSERT INTO mp3_bytes (songs) " + " Values (@songs) ", conn);
    FileStream fs = null;

    string path = fileUpload.FileName;
    fs = new FileStream(path, FileMode.Open, FileAccess.Read);

    Byte[] song = new Byte[fs.Length];
    fs.Read(song, 0, song.Length);
    fs.Close();
    param = new SqlParameter("@songs", SqlDbType.VarBinary, song.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, song);
    cmd.Parameters.Add(param);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

where fileUpload is the file upload control.. I am uploading an mp3 file.. when I execute this I'm getting could not find file '' how can I pass the uploaded filename to filestream from upload file control..

thank you..

+2  A: 

Use fileUpload.PostedFile.FileName Also its better to check if there is any file that has been uploaded using fileUpload.HasFile property. You can also guard against zero length files by checking against fileUpload.PostedFile.ContentLength > 0.

Edited - just realized what wrong you are doing... The uploaded file content needs to saved by you on the disk using fileUpload.PostedFile.SaveAs method. The above file name property will give you file name on client machine but the file will not exist on server. You need to save it wherever you want on server. For example,

var path = Path.Combine(tempDirectory, fileUpload.PostedFile.FileNam);
fileUpload.PostedFile.SaveAs(path);

This would put the uploaded file in temp directory on the server. You can also use PostedFile.InputStream to read file contents.

fs = new FileStream(path, FileMode.Open, FileAccess.Read); will never work as file does not exist on web server machine.

Edit: after you put the sample code Remove FileStream fs = null; and replace

fs = new FileStream(path, FileMode.Open, FileAccess.Read);

with

var fs = uploadFile.PostedFile.InputStream;

and that should do the trick.

VinayC
I tried that too..
Leema
but I need to store an mp3 file as byte[] arrays to the database in that case how can i do this?
Leema
hi thaaaaaanks now it works...
Leema
Happy for you that it works, but you really shouldn't be storing such large binary objects in a database. How about storing them on disk and then just storing the file name / location in the db?
DavidGouge
A: 

If you really do need to use a FileStream, then you'll need to set the FileMode to Create rather than Open as you're not Opening a file, you're Creating one.

fs = new FileStream(path, FileMode.Create);

EDIT: But, as VinayC says, SaveAs() does all the hard work for you!

EDIT: It looks like you are trying to save the file to disk, then read the bytes from the file to pass to sql. You can bypass that saving / reading part by just getting FileBytes from the FileUpload control.

SqlConnection conn = new SqlConnection("Data Source=DDPRO8-WIN7X86\\SQLEXPRESS;Initial Catalog=mp3bytes;Persist Security Info=True;Integrated security=true; User ID=; Password=;");
SqlCommand cmd = null;
SqlParameter param = null;
cmd = new SqlCommand(" INSERT INTO mp3_bytes (songs) " + " Values (@songs) ", conn);

param = new SqlParameter("@songs", SqlDbType.VarBinary, fileUpload.FileBytes.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, fileUpload.FileBytes);
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DavidGouge