1,以流格式读取文件:
FileStream fs = new FileStream("aa.doc", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] DocByte = br.ReadBytes((int)fs.Length);
fs.Close();
2.在页面传文件一般用file控件,方法如下
Int32 intFileLength;
string strFileType;
string strFileName;
Stream stmFile;
strFileName="";
strFileName=FileBox1.PostedFile.FileName;
intFileLength=FileBox1.PostedFile.ContentLength;
strFileType=FileBox1.PostedFile.ContentType;
//strFileType="dd";
stmFile=FileBox1.PostedFile.InputStream;
byte[] ByteFile=new byte[intFileLength];
stmFile.Read(ByteFile,0,intFileLength);
//byte[]可直接赋值给image字段
如: myCom.Parameters.Add("@FileData",SqlDbType.Image);
myCom.Parameters["@FileData"].Value=ByteFile;
FileStream fs = new FileStream("aa.doc", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] DocByte = br.ReadBytes((int)fs.Length);
fs.Close();
2.在页面传文件一般用file控件,方法如下
Int32 intFileLength;
string strFileType;
string strFileName;
Stream stmFile;
strFileName="";
strFileName=FileBox1.PostedFile.FileName;
intFileLength=FileBox1.PostedFile.ContentLength;
strFileType=FileBox1.PostedFile.ContentType;
//strFileType="dd";
stmFile=FileBox1.PostedFile.InputStream;
byte[] ByteFile=new byte[intFileLength];
stmFile.Read(ByteFile,0,intFileLength);
//byte[]可直接赋值给image字段
如: myCom.Parameters.Add("@FileData",SqlDbType.Image);
myCom.Parameters["@FileData"].Value=ByteFile;
<httpRuntime executionTimeout="90" maxRequestLength="100000" useFullyQualifiedRedirectUrl="false" />不过,在B/S下面做,可能会很难吧
另外,我的第二个例子是把已经把文件先以流读到服务器内存里了!!!(stmFile.Read(ByteFile,0,intFileLength);
)
至于后面传到数据库里的,你完全可以不用考虑.
你甚至可以参考一下第一个例子,就可以在服务器建一个临时文件,然后对他操作了,
HttpPostedFile myFile = File1.PostedFile;int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);private void WriteToFile(string strPath/*这里是文件名*/, ref byte[] Buffer)
{
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create); // Write data to the file
newFile.Write(Buffer, 0, Buffer.Length); // Close file
newFile.Close();
}