从数据库表下载在同一记录多个文件 [英] Download multiple files in same record from a database table

查看:122
本文介绍了从数据库表下载在同一记录多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表来存储采购订单数据,它可以容纳多达3个文件(第一个文件是必需的,第二个和第三个是可选的),他们的列名在画面中突出显示。

I have this table to store Purchase Order data, it can hold up to 3 files (first file is required, second and third is optional) which their column names are highlighted in the picture.

在这里输入的形象描述

我已经做了上传页面,它保存所有数据输入并通过用户对数据库的上载,它工作正常,我可以看到表中的所有已保存的文件名和字节。

I have already done the Upload page, which save all the data entered and uploaded by user to the database, it is working fine, I can see all the saved file names and bytes in the table.

我正在查看页面,从数据库中检索数据并显示给网页和有检索文件的问题

I am working on the View page, which retrieve data from database and display it to the webpage and have problem of retrieving the files.

我们的想法是显示文件名页面(这是很好的工作),当用户点击到的文件名,就可以将其保存到计算机(或打开/运行等依赖于网络浏览器的提示窗口) 这里的问题是, 我只能保存第一个文件,在第二和第三个文件名点击时,什么也没发生虽然当在调试模式下,他们(名称,类型和文件2和文件3)的数据确实存在,但该提示窗口保存文件没有出现

The idea is to display the file name to the page (which is working fine) and when user click onto the file name, they can save it to the computer (or open/run etc depend on web browser prompt windows), problem here is that: I can only save the first file, when clicking on the second and third file name, nothing happened although when in debugging mode they (name, type and data of file2 and file3) do exist but the prompt windows to save file is not appeared

不知道如何解决这个问题,或者如果任何人有一个如何下载这些文件更好的办法,请帮助。

Any idea how to fix this or if anyone have better method of how to download these files please help.

下面是我的code(请忽略无关codeS或请让我知道,如果你想知道他们做了什么):

Here is my code (please ignore the unrelated codes or please let me know if you want to know what they do):

该View.aspx显示文件名

The View.aspx to display file names

<asp:LinkButton ID="lbtPOFile" runat="server" OnClick="lbtPOFile_Click"></asp:LinkButton>
<asp:LinkButton ID="lbtPOFile2" runat="server" OnClick="lbtPOFile2_Click"></asp:LinkButton>
<asp:LinkButton ID="lbtPOFile3" runat="server"  OnClick="lbtPOFile3_Click"></asp:LinkButton>

C#的背后:在DownloadFile()方法有3个参数,它们在数据库表中只列名,对应的文件1,文件2或文件3,它设检索文件时,用户点击到的文件名(称为下面的点击事件)

C# behind: the DownloadFile() method has 3 parameters, they are just column names in the database table, corresponding to the file1, file2 or file3, it supposes to retrieve the file when user click onto the file name (called in the click events below)

protected void DownloadFile(string fileNameColumn, string fileTypeColumn, string fileDataColumn)
    {
        string guid = !string.IsNullOrEmpty(Request.QueryString["guid"]) ? Request.QueryString["guid"] : Guid.Empty.ToString();
        string id = !string.IsNullOrEmpty(Request.QueryString["id"]) ? Request.QueryString["id"] : "0";

        if (requestDAL.ValidatePODetailLink(guid, Convert.ToInt32(id)))
        {
            byte[] bytes = null;
            string fileName = "";
            string contentType = "";

            DataTable PODetail = requestDAL.GetPODetail(guid, Convert.ToInt32(id));
            foreach (DataRow row in PODetail.Rows)
            {
                bytes = (byte[])row[fileDataColumn];
                contentType = row[fileTypeColumn].ToString();
                fileName = row[fileNameColumn].ToString();
            }
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = contentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        else
        {
            //Display message
            InfoPanel.Visible = true;
            lblMessage.Text = "<b>Invalid file or an error has occurred while connecting to the database. Please try again later!</b>";
            lblMessage.CssClass = "text text-danger bold";
            InfoPanel.CssClass = "panel panel-danger";
            FormPanel.Visible = false;
            FormPanel.Enabled = false;
        }
    }

protected void lbtPOFile_Click(object sender, EventArgs e)
    {
        try
        {
            DownloadFile("poFileName", "poFileContentType", "poFileData");
        }
        catch (Exception ex)
        {
            //Display message
            InfoPanel.Visible = true;
            lblMessage.Text = "<b>An error has occurred. Please try again later!</b></br>" + ex.Message;
            lblMessage.CssClass = "text text-danger bold";
            InfoPanel.CssClass = "panel panel-danger";
            FormPanel.Visible = false;
            FormPanel.Enabled = false;
        }
    }

    protected void lbtPOFile2_Click(object sender, EventArgs e)
    {
        try
        {
            DownloadFile("poFileName2", "poFileContentType2", "poFileData2");
        }
        catch (Exception ex)
        {
            //Display message
            InfoPanel.Visible = true;
            lblMessage.Text = "<b>An error has occurred. Please try again later!</b></br>" + ex.Message;
            lblMessage.CssClass = "text text-danger bold";
            InfoPanel.CssClass = "panel panel-danger";
            FormPanel.Visible = false;
            FormPanel.Enabled = false;
        }
    }

    protected void lbtPOFile3_Click(object sender, EventArgs e)
    {
        try
        {
            DownloadFile("poFileName3", "poFileContentType3", "poFileData3");
        }
        catch (Exception ex)
        {
            //Display message
            InfoPanel.Visible = true;
            lblMessage.Text = "<b>An error has occurred. Please try again later!</b></br>" + ex.Message;
            lblMessage.CssClass = "text text-danger bold";
            InfoPanel.CssClass = "panel panel-danger";
            FormPanel.Visible = false;
            FormPanel.Enabled = false;
        }
    }

在某些情况下,相关的功能,你需要它:

Some related functions in case you need it:

// Validate link for employee (link format is View.aspx?guid=xxx&id=xxx)
    public static bool ValidatePODetailLink(string guid, int poID)
    {
        using (SqlConnection con = new SqlConnection(CS))
        {
            string query = "SELECT COUNT(*) FROM PO WHERE poID = @poID AND poGUID = @guid";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@guid", guid);
            cmd.Parameters.AddWithValue("@poID", poID);
            con.Open();
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if (i == 1) return true;
            else return false;
        }
    }

    //Get po request details for employee
    public static DataTable GetPODetail(string guid, int poID)
    {
        using (SqlConnection con = new SqlConnection(CS))
        {
            string query = "SELECT * FROM PO WHERE poID = @poID AND poGUID = @guid";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@guid", guid);
            cmd.Parameters.AddWithValue("@poID", poID);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }

发现问题:
我使用的UpdatePanel ,只把&LT;&触发器GT; &LT; ASP:PostBackTrigger控件ID =lbtPOFile/&GT; &LT; /触发器&GT; 因此它缺少 PostBackTrigger lbtPOFile2 lbtPOFile3 。新增的2条线和它的作品。

PROBLEM FOUND: I use UpdatePanel and only put <Triggers> <asp:PostBackTrigger ControlID="lbtPOFile" /> </Triggers> hence it was missing PostBackTrigger for lbtPOFile2 and lbtPOFile3. Added those 2 lines and it works.

推荐答案

我建议以下办法。


  1. 还原您的code将已知良好状态(它只能处理一个文件的下载,但这样做正确)

  1. Revert your code to the "known good" state (where it can only handle the download of one file, but does so correctly)

重构这个code,以便它仍然支持单个文件下载。就拿code的流文件内容到响应;它移动到一个单独的功能(或者叫这个 SENDFILE );叫您点击处理程序的功能;重新编译并确认一切都还在工作。

Refactor this code so that it still supports a single file download. Take the code that streams the file contents into the response; move it into a separate function (perhaps call this SendFile); call that function from your click handler; recompile and confirm that everything is still working.

现在修改 SENDFILE ,使其接受确定它是否会返回文件1,文件2或文件3.修改单击处理程序,使输入参数它通过一遍指示文件1.重新编译和测试参数,确保它仍然有效。

Now modify SendFile so that it accepts input parameters that determine whether it will return file 1, file 2, or file 3. Modify the click handler so that it passes the parameters that indicate file 1. Recompile and test again, make sure it still works.

现在添加两个额外的点击处理程序的另外两个链接按钮。应该是除了传递给 SENDFILE 的参数相同,现有的单击处理程序。重新编译和测试与所有三个文件。

Now add two additional click handlers for your other two link buttons. Should be identical to your existing click handler except for the arguments that are passed to SendFile. Recompile and test with all three files.

这篇关于从数据库表下载在同一记录多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆