大家不要BS,我确实没有C#基础就直接拿它搞开发,所以可能会问些弱智的问题,不过我确实搞了好久没有搞定。项目中让用户可以上传到数据库和从数据库下载文件,这个做好,可后来用提出要在网页中直接打开文件(如CAD的dwg文件在网页中直接浏览)
我的思路是先从数据库中把文件下载到服务器的一个目录中,再通过别的方法实现让用户在网页中浏览。可现在搞了老半天,一直无法把数据从
库从写入本地文件夹内。代码如下,大家帮看一下:
string sql = "select content from wolftest2 where id=6";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
System.IO.FileStream fs;
System.IO.BinaryWriter bw;
int iBufferSize = 1000;
Byte[] outbyte = new Byte[iBufferSize - 1];
long retval;
int startIndex = 0;
string fPath; fPath = Server.MapPath(Request.ApplicationPath) + "\\upLoadFiles\\" + filename;
fs = new System.IO.FileStream(fPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
bw = new System.IO.BinaryWriter(fs); startIndex = 0;
int itemNum = 0;
retval = myReader.GetBytes( startIndex,itemNum, outbyte, 0, iBufferSize); while (retval == iBufferSize)
{
bw.Write(outbyte);
bw.Flush();//清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备
startIndex = startIndex + iBufferSize;
retval = myReader.GetBytes(itemNum, startIndex, outbyte, 0, iBufferSize);
}
//将剩下buffer的内容写到文件中
bw.Write(outbyte);
bw.Flush(); //关闭文件流
bw.Close();
fs.Close(); //关闭reader和数据库连接connection
myReader.Close();
程序执行到红色字体部分就报错了:缓冲区偏移量“0”加上“2979894”可用字节数大于在缓冲区中传递的长度。有没有哪位大哥知道中哪的错误?或有这样的例子给我发一份,小弟先谢过了。我翻了几本书,作者太不敬业,程序里到处是错误,有很多地方都是网上的源码。业务不熟悉害死人啊。。唉
我的思路是先从数据库中把文件下载到服务器的一个目录中,再通过别的方法实现让用户在网页中浏览。可现在搞了老半天,一直无法把数据从
库从写入本地文件夹内。代码如下,大家帮看一下:
string sql = "select content from wolftest2 where id=6";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
System.IO.FileStream fs;
System.IO.BinaryWriter bw;
int iBufferSize = 1000;
Byte[] outbyte = new Byte[iBufferSize - 1];
long retval;
int startIndex = 0;
string fPath; fPath = Server.MapPath(Request.ApplicationPath) + "\\upLoadFiles\\" + filename;
fs = new System.IO.FileStream(fPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
bw = new System.IO.BinaryWriter(fs); startIndex = 0;
int itemNum = 0;
retval = myReader.GetBytes( startIndex,itemNum, outbyte, 0, iBufferSize); while (retval == iBufferSize)
{
bw.Write(outbyte);
bw.Flush();//清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备
startIndex = startIndex + iBufferSize;
retval = myReader.GetBytes(itemNum, startIndex, outbyte, 0, iBufferSize);
}
//将剩下buffer的内容写到文件中
bw.Write(outbyte);
bw.Flush(); //关闭文件流
bw.Close();
fs.Close(); //关闭reader和数据库连接connection
myReader.Close();
程序执行到红色字体部分就报错了:缓冲区偏移量“0”加上“2979894”可用字节数大于在缓冲区中传递的长度。有没有哪位大哥知道中哪的错误?或有这样的例子给我发一份,小弟先谢过了。我翻了几本书,作者太不敬业,程序里到处是错误,有很多地方都是网上的源码。业务不熟悉害死人啊。。唉
fs = new System.IO.FileStream(fPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
fs.write(bytes,0,bytes.length);
fs.flush();
fs.close();
fs.Dispose();