简体   繁体   English

从C#应用程序上载和下载SQL Server上的文件

[英]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. 你可以在数据库表中保存我认为很好的路径。

Here is code for file upload 这是文件上传的代码

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";

            }
}

Here is code for file download 这是文件下载的代码

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.

相关问题 从 C# 中的 SQL 服务器下载/上传文件 [暂停] - Download/upload files from/to SQL Server in C# [on hold] 从WinForms C#app上传/下载SQL Server 2005/2008中的文件? - Upload / Download file from SQL Server 2005/2008 from WinForms C# app? 如何使用C#从服务器上载和下载文件 - How to upload and download file from a server using C# 在 C#/.NET 中向/从 FTP 服务器上传和下载文件 - Upload and download a file to/from FTP server in C#/.NET 无法从SQL Server和C#应用程序在csv中下载具有80列的6到700万条记录 - Not able to download 6 to 7 million records with 80 columns in csv from SQL Server and C# application 如何使用 asp.net c# 从 SQL 服务器管理下载文件? - How to download files from SQL Server Management using asp.net c#? 如何从Outlook收件箱下载Excel附件并将数据保存到C#控制台应用程序中的sql服务器? - How to download an excel attachment from outlook inbox and save the data to sql server in C# Console Application? c#如何将多个文件上传到SQL Server数据库? - c# How to upload multiple files to SQL Server database? 使用C#应用程序从Linux控制台将文件上传到Box - Upload Files to Box from Linux Console using c# application C#从服务器下载特殊字符文件名称文件 - C# download special character file name files from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM