Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ContentType不工作,mime=应用程序/八位字节流_C#_Asp.net_Content Type_Mime Types - Fatal编程技术网

C# ContentType不工作,mime=应用程序/八位字节流

C# ContentType不工作,mime=应用程序/八位字节流,c#,asp.net,content-type,mime-types,C#,Asp.net,Content Type,Mime Types,在C#中,我正在做 context.Response.ContentType = "image/png"; context.RewritePath(sz); context.Response.ContentType = "image/png"; 它似乎重定向了路径,因为我可以在页面上看到图像。然而,在firefox中,当我右键单击并选择“查看图像”时,我会得到一个下载。使用这个C++代码,我可以看到MIME是应用程序/八位

在C#中,我正在做

            context.Response.ContentType = "image/png";
            context.RewritePath(sz);
            context.Response.ContentType = "image/png";
它似乎重定向了路径,因为我可以在页面上看到图像。然而,在firefox中,当我右键单击并选择“查看图像”时,我会得到一个下载。使用这个C++代码,我可以看到MIME是应用程序/八位字节流< /p>
#include <curl/curl.h> 
#include <ios>
#include <iostream>
#include <string>
#pragma comment(lib, "curllib")
using namespace std;
size_t function( void *ptr, size_t size, size_t nmemb, void *stream)
{
    cout << (char*)ptr;
    return size*nmemb;
}
size_t function2( void *ptr, size_t size, size_t nmemb, void *stream)
{
    return size*nmemb;
}
int main()
{
    CURL *c = curl_easy_init();
    curl_easy_setopt(c, CURLOPT_HEADERFUNCTION, function);
    curl_easy_setopt(c, CURLOPT_WRITEHEADER , 1);
    curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, function2);
    curl_easy_setopt(c, CURLOPT_WRITEDATA, 0);
    curl_easy_setopt(c, CURLOPT_URL, "http://localhost:3288/s/main/4/AAA.png");
    curl_easy_perform(c);
    curl_easy_cleanup(c);
    return 0;
}
#包括
#包括
#包括
#包括
#pragma注释(lib,“curllib”)
使用名称空间std;
大小函数(void*ptr、size\t size、size\t nmemb、void*stream)
{
cout签出:


它应该为您提供一个将PNG转储到响应中的示例。

当您使用
重写路径
时,您写入头部的内容将被丢弃。处理新路径的内容将提供头部


如果要指定内容类型,则不能使用
RewritePath
。相反,可以使用
Response.BinaryWrite
Response.WriteFile
在设置标头后将文件数据输出到响应流。

应用程序/octet流
将告诉浏览器该文件是二进制文件,并且您的browser随后将下载它

这在过去对我很有效(在一个只负责提供文件的
HttpHandler
中,因此使用clear调用):


Response.Clear();
Response.ClearHeaders();
Response.ContentType=“image/png”;
AddHeader(“内容类型”,图像/png”);

如果确实要下载该文件,还可以添加:


AddHeader(“内容处置”、“附件;文件名=myimage.png”);

向浏览器建议要另存为的文件名

Response.Clear(); Response.ClearHeaders(); Response.ContentType = "image/png"; Response.AddHeader("Content-Type", image/png"); Response.AddHeader("Content-Disposition", "attachment; filename=myimage.png");