int pdfMax = 0;
try
{
pdfMax = Convert.ToInt32(ConfigurationManager.AppSettings["PDFMax"]);
}
catch
{
pdfMax = 5;
}
this.openFileDialog1.Filter = "PDF檔案|*.PDF";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.StrFile = this.openFileDialog1.FileName;
this.txtFile.Text = this.StrFile.Substring(this.StrFile.LastIndexOf('\\') + 1);
FileInfo fi = new FileInfo(this.StrFile);
FileStream fs=fi.OpenRead();
long fileLen = fs.Length;
if (fileLen > pdfMax * 1024 * 1024)
{
MessageBox.Show("上傳文件限制最大" + pdfMax + "MB");
return;
}
byte[] bytes = new byte[fileLen];
fs.Read(bytes, 0, (int)fileLen);
DataSet ds = new DataSet();
string sql = "insert into tPic values(1,@Pic)";
using (SqlConnection cn = new SqlConnection("Data Source=dev755;Initial Catalog=Test;User ID=sa;Password=liteon1234"))
{
using (SqlCommand cm = new SqlCommand(sql))
{
if (cn.State != ConnectionState.Open)
cn.Open();
cm.Connection = cn;
SqlParameter par = new SqlParameter("@Pic", SqlDbType.Image);
par.Value = bytes;
cm.Parameters.Add(par);
cm.ExecuteNonQuery();
cm.CommandText = "Select*From tPic";
cm.Parameters.Clear();
using (SqlDataAdapter sa = new SqlDataAdapter(cm))
sa.Fill(ds);
}
}

if (ds == null) return ;
if (ds.Tables.Count == 0) return ;
byte[] outs= (byte[])ds.Tables[0].Rows[0]["Pic"];
FileStream fs1 = new FileStream(@"D:\網絡訪問異常處理3.pdf", FileMode.OpenOrCreate,FileAccess.ReadWrite);
fs1.Write(outs, 0, outs.Length);
fs1.Close();
WebBrowser wb = new WebBrowser();
wb.Navigate(@"D:\網絡訪問異常處理3.pdf", true);
DirectoryInfo di = new DirectoryInfo(@"D:\");
FileInfo[] fis=di.GetFiles();
for(int i = 0; i < fis.Length; i++)
{
if (fis[i].CreationTime.ToString("yyyy-MM-dd").CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
File.Delete(fis[i].FullName);
}

posted on 2021-10-22 17:01  Hawk_Yuan  阅读(168)  评论(0编辑  收藏  举报