【问题标题】:I have created a download link. But the downloaded file is missing its filetype (A plain file is downloaded every time)我创建了一个下载链接。但是下载的文件缺少它的文件类型(每次都下载一个普通文件)
【发布时间】:2019-08-23 06:47:44
【问题描述】:
protected void downloadbtn(object sender, EventArgs e)
{
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;
    using (SqlConnection con = new SqlConnection(str))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Name, Data, Type from demons where ID=@Id";
            cmd.Parameters.AddWithValue("@ID", id);
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                bytes = (byte[])sdr["Data"];
                contentType = sdr["Type"].ToString();
                fileName = sdr["Name"].ToString();
            }
            con.Close();
        }
    }
    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();
}

//文件类型不随文件一起发送 // 例如,如果要下载一张照片,则将下载一个普通文件,然后您需要专门使用绘画或照片应用程序打开它

【问题讨论】:

  • “类型丢失”是什么意思? contentType 变量的内容是什么?
  • 它询问“另存为类型=所有文件”而不是 .JPEG 或 .MP4 或 .txt
  • 好的,这是第一个问题,第二个问题?
  • 只有一个问题。
  • 我问了 2 个问题,如果您希望人们帮助您,请花点时间阅读。

标签: c# hyperlink download


【解决方案1】:

我相信您的问题是 contentType 变量中存储的内容不是有效的 MIME 类型。

您可以在此处查看已知的 Windows MIME 类型列表https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775147(v=vs.85)

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多