filestream上傳
Ⅰ 文件上傳
上傳文件時首先通過上傳控制項找到所需上傳的文件,然後獲取文件的大小,最後以流的形式寫入資料庫,具體代碼為:
private void Btn_OK_Click(object sender, System.EventArgs e)
{
string name=name_TextBox.Text;
//接收上傳文件
Stream fileStream=File1.PostedFile.InputStream;
//獲取上傳文件位元組的大小
int length=File1.PostedFile.ContentLength;
byte[] wordData=new byte[length];
//從流中讀取位元組並寫入wordData
int n=fileStream.Read(wordData,0,length);
//獲取當前時間
DateTime time=DateTime.Now;
//連接資料庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="workstation id=TIANCHUNZHU;packet size=4096;integrated security=SSPI;data source=TIANCHUNZHU;persist security info=False;initial catalog=test";
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandText="INSERT INTO word (fileName,postTime,fileContent) values (@fileName,@postTime,@fileContent)";
SqlParameter nameParam=new SqlParameter("@fileName",System.Data.SqlDbType.VarChar,50);
nameParam.Value=name;
cmd.Parameters.Add(nameParam);
SqlParameter timeParam=new SqlParameter("@postTime",System.Data.SqlDbType.DateTime,8);
timeParam.Value=time;
cmd.Parameters.Add(timeParam);
//添加word文件
SqlParameter contentParam=new SqlParameter("@fileContent",System.Data.SqlDbType.Image); ①//見本段最後註解
contentParam.Value=wordData;
cmd.Parameters.Add(contentParam);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
注①:此處由於是Image類型文件,事先可能無法預測文件的大小,因此可不必指定size參數。如果希望控制上傳文件的大小則可以輸入size參數。如指定1000,則上傳時最大可以上傳1k的word文檔。
(3) 從資料庫中讀取數據並恢復為word文件
讀取數據時先將數據從資料庫中讀入緩沖區,然後再從緩沖區寫入最終文件。因此首先要開辟一個緩沖區並設定它的大小,每當緩沖區讀滿時就要將緩沖區內的數據寫入文件,以清空緩沖區並繼續向緩沖區讀數據,直到最後一次將緩沖區內剩餘的數據全部寫入文件,新的word文檔即可生成。
由於這一部分用到了位元組流的輸入輸出操作,因此要引用System.IO命名空間
下面是關於這一部分的完整代碼:
private void Btn_get_Click(object sender, System.EventArgs e)
{
//連接資料庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="workstation id=TIANCHUNZHU;packet size=4096;integrated security=SSPI;data source=TIANCHUNZHU;persist security info=False;initial catalog=test";
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
//根據TextBox中指定的文件名進行查找讀取
cmd.CommandText="select fileContent from word where fileName='"+name_TextBox.Text.ToString()+"'";
FileStream fs;
BinaryWriter bw;
//設定允許讀取到緩沖區的最大長度
int buffersize=100;
//要將位元組流讀入的緩沖區
byte[] outbyte=new byte[buffersize];
//用於記錄已經讀取的位元組數
long reval;
//欄位中的索引,從這里開始讀取操作
long startIndex;
//FileStream對象將封裝的文件的相對路徑或絕對路徑
string filePath=@"C:\wordData.doc";
conn.Open();
SqlDataReader reader;
reader=cmd.ExecuteReader();
while (reader.Read())
{
fs=new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write);
bw=new BinaryWriter(fs);
startIndex=0;
//將位元組流讀入outbyte緩沖區中並返回讀取的位元組數
reval=reader.GetBytes(0,startIndex,outbyte,0,buffersize);
//當讀取的位元組流達到緩沖區允許的最大長度時要卸載緩沖區內的數據並將數據寫入文件
while (reval==buffersize)
{
bw.Write(outbyte);
bw.Flush();
//重新設定開始讀取的位置,並繼續讀取和寫數據
startIndex+=buffersize;
reval=reader.GetBytes(0,startIndex,outbyte,0,buffersize);
}
//將緩沖區內最後剩餘的數據寫入文件
bw.Write(outbyte,0,(int)reval-1);
bw.Flush();
bw.Close();
fs.Close();
}
reader.Close();
conn.Close();
}
此時將按照filePath中指定的路徑和名稱重新生成word文檔。可以在filePath中根據具體情況指定生成的word文檔的名稱和路徑。
Ⅱ winform怎麼上傳文件到伺服器,能否發送一段代碼,初學者,相當感謝~
看看這個,希望對你有用
Ⅲ C# winform如何實現批量上傳文件到遠程伺服器
基本思路是遠程伺服器假設FTP,本地用System.Net.FtpWebRequest建立Ftp客戶端,用NetworkCredential.NetworkCredential建立用戶名和密碼驗證方式,用System.Net.WebRequestMethods.Ftp設置上傳和下載命令,文件通過FileStream流進行發送和接收。
Ⅳ asp.net C#如何上傳指定路徑的圖片,不用FileUpload控制項
/////////////////////////////
FileStream
tfs
=
File.Open(filepath,FileMode.Open);
BinaryReader
by
=
new
BinaryReader(tfs);
byte[]
outB
=
new
byte[(int)tfs.Length];
by.Read(outB,
0,
(int)tfs.Length);
tfs.Close();
by.Close();
//////////////////////////////
outB就是上傳文件的2進制流,接下來想怎樣弄就怎樣弄了。
Ⅳ c#如何實現將文件上傳到伺服器求詳細代碼謝了
//文件寫入流
private void ReadFile()
{
Byte[] MesageFile;
string path =@"c:\123.XML";
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
int size = Convert.ToInt32(stream.Length);
MesageFile = new Byte[size];
stream.Read(MesageFile, 0, size);
stream.Close()
string fileName =path.Substring(path.LastIndexOf("\\") + 1, path.Length path.LastIndexOf("\\") - 1);
WriteFile(MesageFile, fileName);
}
//寫入文件
private void WriteFile(Byte[] fileByte,string fileName)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\UpLoad\\" + DateTime.Now.ToString("yyyy-MM-dd")+"\\";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string savepath = path + fileName;
FileStream fos = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fos.Write(MesageFile, 0, MesageFile.Length);
fos.Close();
}
上傳的文件格式不限。
Ⅵ 為什麼FTP上傳文件使用FTPClient的appendFileStream方法在斷點續傳時返回輸出流為空,正常上傳時不為null
nt iBytes = soketData.Receive(buffer, bytesRead, 0); // 套接字讀取正確使用方法很多,但是沒有隻帶兩個int類型參數的方法,正確使用:int iBytes = soketData.Receive(buffer);// buffer ---- 讀取緩存。// iBytes ---- 實際接收的位元組數。