上传word文件
public int SaveOrUpdatePhoto(FileUpload FileUp, string TdDh, string QyID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int FileLength = FileUp.PostedFile.ContentLength;
string FileType = FileUp.PostedFile.ContentType;
if (FileLength == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 2;
}
else if (FileType != "application/msword")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 3;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] Document = new byte[FileLength];
Stream ObjectStream = FileUp.PostedFile.InputStream;
int readedLengh = ObjectStream.Read(Document, 0, FileLength);
string Type = FileUp.PostedFile.ContentType;
if (readedLengh != FileLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 0;
}
Connection connstr = new Connection();
string cnstr = connstr.GetConnectionString();
SqlConnection cn = new SqlConnection(cnstr);
cn.Open();
string sql = "update TDJBXXB Set XcApDocument = @Document Where QYID=@QyID And TDDH = @TdDh";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.CommandType = CommandType.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cmd.Parameters.Add("@TdDh", SqlDbType.VarChar, 15).Value = TdDh;
cmd.Parameters.Add("@Document", SqlDbType.Image, FileLength).Value = Document;
cmd.Parameters.Add("@QyID", SqlDbType.VarChar, 30).Value = QyID;
cmd.Parameters["@TdDh"].Direction = ParameterDirection.Input;
cmd.Parameters["@Document"].Direction = ParameterDirection.Input;
cmd.Parameters["@QyID"].Direction = ParameterDirection.Input;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cmd.ExecuteNonQuery();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ObjectStream.Flush();
ObjectStream.Dispose();
ObjectStream.Close();
cn.Close();
return 1;
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "DataAccess Layer Exception Policy");
if (rethrow)
throw;
return 0;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从数据库下载word文件
public int GetXcApDocument(string QyID, string TdDh, Page page)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Connection con = new Connection();
string cnstr = con.GetConnectionString();
SqlConnection conn = new SqlConnection(cnstr);
string sql = "Select XcApDocument From TDJBXXB Where QYID=@QYID And TDDH=@TDDH";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@QYID", SqlDbType.VarChar).Value = QyID;
cmd.Parameters.Add("@TDDH", SqlDbType.VarChar).Value = TdDh;
cmd.Parameters["@QYID"].Direction = ParameterDirection.Input;
cmd.Parameters["@TDDH"].Direction = ParameterDirection.Input;
FileStream fs;
BinaryWriter bw;
//设定允许读取到缓冲区的最大长度
int buffersize = 100;
//要将字节流读入的缓冲区
byte[] outbyte = new byte[buffersize];
//用于记录已经读取的字节数
long reval;
//字段中的索引,从这里开始读取操作
long startIndex;
//FileStream对象将封装的文件的相对路径或绝对路径
string filePath = null;
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();
if (!reader.HasRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 2;
}
while (reader.Read())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
filePath = page.Request.PhysicalApplicationPath + "../行程安排.doc";
fs = null;
bool locked = true;
while (locked)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
locked = false;
bw = new BinaryWriter(fs);
startIndex = 0;
//将字节流读入outbyte缓冲区中并返回读取的字节数
reval = reader.GetBytes(0, startIndex, outbyte, 0, buffersize);
//当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件
while (reval == buffersize)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Threading.Thread.Sleep(100);
}
}
}
reader.Close();
conn.Close();
GetDocument(filePath, page);
return 0;
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "DataAccess Layer Exception Policy");
if (rethrow)
throw;
return 1;
}
}
private void GetDocument(string filename, Page page)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//string path = Server.MapPath(this.xlfile.Text + ".xls");
System.IO.FileInfo file = new System.IO.FileInfo(filename);
page.Response.Clear();
page.Response.Charset = "GB2312";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
page.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
page.Response.ContentType = "application/octet-stream";
// 把文件流发送到客户端
page.Response.WriteFile(file.FullName);
// 停止页面的执行
page.Response.End();
}