1. 上傳doc後,讀入bytes至陣列中,儲存bytes[]至資料庫的varbinary(MAX)。
2. 輸出的時候,從varbinary(MAX)讀出bytes[],response輸出二進位字元,設定標頭為"Content-disposition", "inline; filename=xxx.doc", contenttype為application/msword。
寫入
protected void UploadButton_Click(Object sender , EventArgs e ){ // Handles UploadButton.Click
Stream stream = FileUpload1.FileContent;
BinaryReader binReader = new BinaryReader(stream);
byte[] bytes = new byte[stream.Length];
int numBytesToRead = (int)stream.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = stream.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
}
public string insertDoc(byte[] bytes)
{
.....
sqlCommand.Parameters.Add("@doc", SqlDbType.VarBinary, -1).Value = bytes;
......
}
輸出
protected void Page_Load(object sender, EventArgs e)
{
DBAccess dba = new DBAccess();
byte[] docContent = dba.getDoc();
string fileName = "kkk我.doc";
if (Request.Browser.Browser == "IE")
{
fileName = Server.UrlPathEncode(fileName);
}
Response.ContentType = "application/msword";
Response.AppendHeader("Content-disposition", "inline; filename=" + fileName);
Response.BinaryWrite(docContent);
}
No comments:
Post a Comment