[英]Upload and download files on SQL Server from a C# application
I have a C# application that is uploading files to a sql server, I use this code to get the pdf file and then I change it to "bytes" for upload on the SQL Server database. 我有一个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);
}
}
I use the code below to upload the file: 我使用下面的代码上传文件:
if (!mConnector.Update("INSERT INTO **** (***, ***, CV) " +
"VALUES ('" + *** + "', '" + *** + "', '" + bytes + "')"))
{
Messages.errorMessageBox("This CV already exists.");
}
else
{
ChangeScreen(ActiveScreen, ActiveScreens.ActiveScreen_CVList);
}
But now I don't know how to download this file and how to make a pdf file with the data stored on the database to see it. 但现在我不知道如何下载此文件以及如何使用存储在数据库中的数据制作pdf文件以查看它。 Can anyone help me?
谁能帮我?
Thanks! 谢谢!
First off, let's change the way you are forming your insert statement so you aren't opening up your system to sql injection. 首先,让我们改变你形成insert语句的方式,这样你就不会打开你的系统来进行sql注入。 This will also make the insert statement easier to work with
这也将使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();
To read the data out, use ExecuteReader, then the File object to save it to the disk. 要读取数据,请使用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();
I suggest you to insert your byte data using SqlParameters... see Inserting a byte array into sql server 我建议你使用SqlParameters插入你的字节数据...请参阅将字节数组插入sql server
Then, read the record using SqlDataReader's GetBytes(...)
function see here . 然后,使用
SqlDataReader's GetBytes(...)
函数读取记录,请参见此处 。
I suggest you to upload pdf and save it in separate folder. 我建议你上传pdf并将其保存在单独的文件夹中。 you can save the path in database table that I think it is good.
你可以在数据库表中保存我认为很好的路径。
Drag “Fileupload” control to .aspx page (Use this code is for save .PDF to folder) 将“Fileupload”控件拖到.aspx页面(使用此代码用于保存.PDF到文件夹)
protected void fileUpload()
{
if (fileUp.HasFile)
{
fileUp.SaveAs(Server.MapPath("~/PoPDF/" + this.txtCusPo.Text +".PDF"));
string imgPrintPo = this.txtCusPo.Text + ".PDF";
}
}
You can put this code in button event but here I have used GridView row command event. 您可以将此代码放在按钮事件中,但在这里我使用了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.