如果使用asp:fileupload字段,则从数据库下载文件会挂起IE

时间:2014-10-09 13:00:28

标签: c# asp.net file-upload download

IE Version 11

如果我做的第一件事就是下载一个文件就行了。但是,如果我使用asp:fileupload字段IE并选择我要下载的文件后我提交表单,之后我想使用下载链接从我的sql数据库下载文件IE只是永远加载而我不能按任何东西。 (它适用于最新的Firefox和Chrome)

这是HTML标记,下载链接的Repeater和asp:fileupload:

<asp:Repeater ID="RepeaterDatein" runat="server" OnItemCommand="RepeaterDatein_ItemCommand">
            <ItemTemplate>  
                <asp:LinkButton runat="server" class="downloadfile" ID="LinkButton1" CommandArgument='<%# Eval("dataId") %>' CommandName="download"><%# DataBinder.Eval(Container.DataItem, "dataName") %></asp:LinkButton><br />
            </ItemTemplate>
       </asp:Repeater>

    <!-- Upload Multiple Files -->
    <div id="fileUploadarea"><asp:FileUpload ID="fuPuzzleImage" runat="server" CssClass="fileUpload" /></div>

这是在触发ItemCommand时从数据库下载文件的c#代码:

 try {
            if (e.CommandName == "download") {
               Arbeitsmeldung hinzugefügt wird
                string id = e.CommandArgument.ToString();
                string fileName;
                byte[] bytes;

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "Select dataName,dataFile FROM datein WHERE dataId = @dataId";

                cmd.Parameters.AddWithValue("@dataId", id);
                cmd.Connection = conn;

                conn.Open();


                using (SqlDataReader sdr = cmd.ExecuteReader()) {
                    sdr.Read();
                    fileName = sdr["dataName"].ToString();
                    bytes = (byte[])sdr["dataFile"];
                }

                conn.Close();

                Response.Clear();   
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(bytes);
                Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();  
            }
        }
        catch (Exception ex) {
            this.Session["exceptionMessage"] = ex.Message;
            HttpContext.Current.Server.Transfer("Error.aspx");
        }

点击asp:FileUpload,浏览数据并选择文件后会发生什么。在我按下提交按钮之前没有上传,浏览器存储了什么样的数据以及如何与filedownload冲突?

1 个答案:

答案 0 :(得分:0)

您必须将此代码放入下载按钮.....

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + dt.Rows[0]["v_file"]);
Response.ContentType = "application/octet-stream";
Response.WriteFile(Server.MapPath("upload/") + dt.Rows[0]["v_file"]);
Response.End();