【问题标题】:Saving Data to Database using fileuploadControl in C#在 C# 中使用 fileuploadControl 将数据保存到数据库
【发布时间】:2017-01-27 17:27:30
【问题描述】:

我的表单中有一个多文件上传控件。我正在尝试上传尽可能多的用户想要使用下拉列表选择附件类型的数据库的文件。我可以将文件保存到数据库,但我的数据全为 0(0x0000000000000000000000000000000000000000...00)。 当我试图在用户单击文件链接时检索文件时,我收到一个错误,即“无法加载 pdf 文档”。

这是我将其保存到数据库的代码:

    public void getDynamicFilesFromUploadedFileList(int wWireTransferID, int wUserID)
    {
        if (GridView1.Rows.Count > 0)
        {
            foreach (GridViewRow grow in GridView1.Rows)
            {
                try
                {
                    FileUpload fileuploadControl = new FileUpload();
                    fileuploadControl = (FileUpload)grow.FindControl("fUpControl");

                    DropDownList drpFileType = new DropDownList();
                    drpFileType = (DropDownList)grow.FindControl("ddlFileType");
                    Guid newID = Guid.NewGuid();

                    if (fileuploadControl.HasFile)
                    {
                       //I believe here is my mistake
                        Byte[] fileData = new Byte[fileuploadControl.PostedFile.ContentLength];

                        new MRSManager().InsertWTDocument(wWireTransferID,
                             fileuploadControl.FileName,
                             drpFileType.SelectedValue.Trim(),
                             fileuploadControl.PostedFile.ContentType,
                             System.IO.Path.GetExtension(fileuploadControl.FileName),
                             fileuploadControl.PostedFile.ContentLength.ToString(),
                             fileData,
                             wUserID,
                             newID);
                    }
                    else
                    {
                        string m = "No File! ";
                    }
                }
                catch (Exception Exp)
                {
                    string msg = Exp.Message.ToString();
                }
            }
        }
    }

这是我如何尝试检索文件的代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get the file id from the query string
        string id = Request.QueryString["ID"];

        // Get the file from the database
        DataTable file = GetAFile(id);
        DataRow row = file.Rows[0];

        string name = (string)row["FileName"];
        string contentType = (string)row["ContentType"];
        Byte[] data = (Byte[])row["Data"];

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        // Send the file to the browser
        Response.AddHeader("Content-type", contentType);
        Response.AddHeader("Content-Disposition", "inline; filename=" + name);
        Response.Buffer = true;
        Response.BinaryWrite(data);
        Response.Flush();
        Response.Close();
        Response.End();
    }

    // Get a file from the database by ID
    public static DataTable GetAFile(string id)
    {
        DataTable file = new DataTable();
        string sSQL = string.Format("SELECT * FROM tblWireTransferDocuments WHERE FileID = '{0}'", id);
        file = SqlHelper.ExecuteDataset(DBConnection.GetConnectionString(), CommandType.Text, sSQL, null).Tables[0];
        return file;
    }
}

}

【问题讨论】:

标签: c# asp.net sql-server database file-upload


【解决方案1】:

你是对的

 //I believe here is my mistake
 Byte[] fileData = new Byte[fileuploadControl.PostedFile.ContentLength];

您只创建了 fileData,但没有填充它。您应该使用 fileuploadControl.FileContent 来填充 fileData。

【讨论】:

  • 您可以创建流并从中读取。使用(System.IO.Stream myStream = fileuploadControl.FileContent) { myStream.Read(fileData, 0, fileuploadControl.PostedFile.ContentLength); }
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2015-08-04
相关资源
最近更新 更多