簡體   English   中英

從C#應用程序上載和下載SQL Server上的文件

[英]Upload and download files on SQL Server from a C# application

我有一個C#應用程序正在將文件上傳到sql server,我使用此代碼獲取pdf文件,然后將其更改為“bytes”以便在SQL Server數據庫上傳。

private void mButtonAddCV_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "PDF Files | *.pdf";
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if (openFileDialog1.FileName.Length > 0)
        {
            pathCV = openFileDialog1.FileName;
        }
    }

    // Read the file and convert it to Byte Array
    string filePath = pathCV;
    string contenttype = String.Empty;

    contenttype = "application/pdf";

    if (contenttype != String.Empty)
    {
        Stream fs = File.OpenRead(filePath);
        BinaryReader br = new BinaryReader(fs);
        bytes = br.ReadBytes((Int32)fs.Length);
    }
}

我使用下面的代碼上傳文件:

if (!mConnector.Update("INSERT INTO **** (***, ***, CV) " +
                            "VALUES ('" + *** + "', '" + *** + "', '" + bytes + "')"))
{
    Messages.errorMessageBox("This CV already exists.");
}
else
{
    ChangeScreen(ActiveScreen, ActiveScreens.ActiveScreen_CVList);
}

但現在我不知道如何下載此文件以及如何使用存儲在數據庫中的數據制作pdf文件以查看它。 誰能幫我?

謝謝!

首先,讓我們改變你形成insert語句的方式,這樣你就不會打開你的系統來進行sql注入。 這也將使insert語句更易於使用

var command = new SqlCommand("INSERT INTO myTable (x, y, z) VALUES (@a, @b, @c)", sqlConnection);
command.Parameters.Add(new SqlParameter("@a", bytes));
command.Parameters.Add(new SqlParameter("@b", bValue));
command.Parameters.Add(new SqlParameter("@c", bValue));

var resultingRows = command.ExecuteNonQuery();

要讀取數據,請使用ExecuteReader,然后使用File對象將其保存到磁盤。

var command = new SqlCommand("Select a from myTable", sqlConnection);
var reader = command.ExecuteReader();
reader.Read();
var pdfBinaryBuffer = (byte[])reader[0];

// Save file to disk
var file = File.Create("myFile.pdf", pdfBinaryBuffer.Length);
file.Write(pdfBinaryBuffer, 0, pdfBinaryBuffer.Length);
file.Close();

我建議你使用SqlParameters插入你的字節數據...請參閱將字節數組插入sql server

然后,使用SqlDataReader's GetBytes(...)函數讀取記錄,請參見此處

我建議你上傳pdf並將其保存在單獨的文件夾中。 你可以在數據庫表中保存我認為很好的路徑。

這是文件上傳的代碼

將“Fileupload”控件拖到.aspx頁面(使用此代碼用於保存.PDF到文件夾)

protected void fileUpload()
{
 if (fileUp.HasFile)
            {

                fileUp.SaveAs(Server.MapPath("~/PoPDF/" + this.txtCusPo.Text +".PDF"));
                string imgPrintPo = this.txtCusPo.Text + ".PDF";

            }
}

這是文件下載的代碼

您可以將此代碼放在按鈕事件中,但在這里我使用了GridView行命令事件。

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
   GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   if (e.CommandName == "SelectDownload")
   {

      Response.Clear();
      Response.ContentType = "application/octet-stream";
      Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
      Response.TransmitFile(Server.MapPath("~/PoPDF/") + e.CommandArgument);
        //Response.Flush();
             Response.End();
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM