المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Getting File Path



C# Programming
08-19-2012, 12:01 PM
Hi all,
I'm trying to create an app that lets people upload their resume to my database. I've seen tutorials for doing this and they all say the same thing. Number 1 save the original name and file type of the file to be uploaded and convert the file to byte array. Number 2 upload to database. However there is a problem, they all seem to assume one thing and that is, they assume the path of the file to be uploaded is known. Just to show you what I'm talking about below is one of the methods I've found that let you upload a file to the database.

protected void InsertDoc_Click(object sender, EventArgs e) { // Read the file and convert it to Byte Array string filePath = Server.MapPath("APP_DATA/TestDoc.docx"); string filename = Path.GetFileName(filePath); FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); br.Close(); fs.Close(); //insert the file into database string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-word"; cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; InsertUpdateData(cmd); }
The line
string filePath = Server.MapPath("APP_DATA/TestDoc.docx");

is supposed to give you the file path but it assumes you already know what it is. I tried to get around this problem by using a FileUpload control and use fileupload1.PostedFile.FileName, but it only returns the file name and extension, not the full path. I have also tried Path.GetFileName( fileupload1.FileName) with no luck. How do I get around this problem? Thanks in advance for replying.