【问题标题】:How to Remove default underscore in asp.net while downloading the file下载文件时如何删除asp.net中的默认下划线
【发布时间】:2021-04-15 07:36:32
【问题描述】:
protected void DownloadFile(object sender, EventArgs e)
        {
            string param = Request.QueryString["id"];
            //int id = int.Parse((sender as LinkButton).CommandArgument);
            //byte[] bytes;
            string fileName;
            SqlCommand cd = DbCon._dbConnection.CreateCommand();
            cd.CommandType = CommandType.Text;
            cd.CommandText = "select id from student_registration where id = '" + param + "'";
            cd.ExecuteNonQuery();
            SqlCommand cmd = DbCon._dbConnection.CreateCommand();
            cmd.CommandText = "select books_pdf from books where id = '" + param + "'";
            // cmd.Parameters.AddWithValue("@Id", param);
            String s1 = cmd.ExecuteScalar().ToString();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                //bytes = (byte[])sdr["Data"];
                //contentType = sdr["ContentType"].ToString();
                fileName = sdr["books_pdf"].ToString();
                
                    }
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "/";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.ContentType = contentType;
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
            //Response.AppendHeader("Content-Disposition", "attachment; filename="  +fileName);

           // Response.TransmitFile(Server.MapPath("~/Files/"+ fileName));


            //  Response.WriteFile(file.FullName);
            Response.Flush();
            Response.End();
        }

这是我为下载文件而编写的代码。但结果是什么得到了正确的文件,但 '/' 被替换为 'underscore; 我的问题是如何删除该下划线并添加'/'代替下划线我已附上图片请检查并帮助我找到解决方案enter image description here

enter image description here 图片显示了存储在数据库中的路径和我要下载的错误

【问题讨论】:

  • 文件名中不允许使用字符/。无效的文件名字符是/ \ : * ? " < > |

标签: asp.net asp.net-core asp.net-web-api


【解决方案1】:

fileName = "../librarian/" + sdr["books_pdf"].ToString();

                }


        WebClient req = new WebClient();
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearContent();
        response.ClearHeaders();
        response.Buffer = true;
        response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(fileName) + "\"");
        byte[] data = req.DownloadData(Server.MapPath(fileName));
        response.BinaryWrite(data);
        response.End();

后来没有给出正确的文件选择,我更正了正确的文件位置,而不是得到了正确的输出

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2013-11-10
    • 1970-01-01
    • 2019-02-03
    • 2021-01-22
    • 2016-04-15
    • 2016-04-04
    • 2016-08-26
    • 2017-03-23
    相关资源
    最近更新 更多