2013-05-27 85 views
1

我有要求,我有文件上傳和按鈕名稱的...
沒有使用瀏覽,我想加載一些默認文件在文件上傳,假設我有c:\serverfloder文件夾和我有圖像"image1.jpg"在..當用戶請求1次,我想automatically load the file into file uploader ..
根據用戶要求,我們給機會改變文件文件....我不想限制用戶。如何自動加載文件到文件上載與瀏覽

+0

出於安全原因,您無法這麼做。想象一下,如果你可以指定用戶上傳的硬盤上的任何文件。如果用戶不知道該文件,就可以輕鬆上傳個人文件。 – user707727

回答

0

你可以參考下面的代碼,我寫了我的申請

string path = FileUpload1.PostedFile.FileName.ToString(); 
     string FileName = FileUpload1.FileName.ToString(); 
     string FileSize = FileUpload1.PostedFile.ContentLength.ToString(); 
     string FileContent = FileUpload1.PostedFile.ContentType.ToString(); 
     string filename = Path.GetFileName(path); 
     FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); 
     BinaryReader br = new BinaryReader(fs); 
     Byte[] bytes = br.ReadBytes((Int32)fs.Length); 
     br.Close(); 
     fs.Close(); 
     string strQuery = "INSERT INTO [testdb1].[dbo].[tblAttachment]([f_name],[fileSize],[attachment],[contentType]) VALUES(@f_name,@fileSize,@attachment,@contentType)"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     cmd.Parameters.Add("@f_name", SqlDbType.VarChar).Value = FileName; 
     cmd.Parameters.Add("@fileSize", SqlDbType.VarChar).Value = Convert.ToInt32(FileSize); 
     cmd.Parameters.Add("@attachment", SqlDbType.Binary).Value = bytes; 
     cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = FileContent; 

並參觀這裏File Opeartions in C#和他們將要或直接嘗試this..will直接存儲你的圖像特定的文件夾其他方法將文件保存到文件夾

if (FileUpload1.HasFile) 

    { 
      FileUpload1.SaveAs(@"D:\MVC Application Demos\FileOpearationsDemo\FileOpearationsDemo\Files\" + FileUpload1.FileName); 

     }