繁体   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