当前位置:首页 » 文件管理 » filestream上传

filestream上传

发布时间: 2023-07-27 10:33:50

Ⅰ 文件上传

上传文件时首先通过上传控件找到所需上传的文件,然后获取文件的大小,最后以流的形式写入数据库,具体代码为:

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 ---- 实际接收的字节数。

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:433
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:147
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:240
java驼峰 发布:2025-02-02 09:13:26 浏览:652
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726