ContentType不起作用,mime = application / octet-stream

时间:2021-08-12 21:23:30

In C# i am doing

在C#我正在做

            context.Response.ContentType = "image/png";
            context.RewritePath(sz);
            context.Response.ContentType = "image/png";

It seems to redirect the path as i can see see images on my pages. However in firefox when i right click and select "view image" i get a download. Using this c++ code i can see the mime being application/octet-stream

它似乎重定向路径,因为我可以看到我的页面上看到的图像。但是在Firefox中,当我右键单击并选择“查看图像”时,我得到一个下载。使用这个c ++代码我可以看到mime是application / octet-stream

#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;
}

My question is how do i set the mime to the correct type? Above i hardcode it to png which doesnt work. However i rather not hardcode any mime and have it work. I was able to redirect an XML file to somefile.aspx and have that the page set the content type and generate the XML. However rewriting to the image path seems to let the image work i just dont get the correct type.

我的问题是如何将mime设置为正确的类型?上面我把它硬编码到png哪个不起作用。但是我宁愿不对任何哑剧硬编码并让它工作。我能够将XML文件重定向到somefile.aspx,并让页面设置内容类型并生成XML。然而,重写到图像路径似乎让图像工作,我只是没有得到正确的类型。

How do i correct the content/mime type? As plan B i'll rewrite to a aspx file. Whats the best way to send an image? Could i do something like context.Response.OutputStream=stream("filename"); ?

我如何更正内容/哑剧类型?作为计划B,我将重写为aspx文件。什么是发送图像的最佳方式?我可以做像context.Response.OutputStream = stream(“filename”); ?

3 个解决方案

#1


When you use RewritePath, whatever you have written to the head is discarded. Whatever is handling the new path is providing the header.

当您使用RewritePath时,无论您写入头部的任何内容都将被丢弃。无论处理新路径是什么,都提供标题。

If you want to specify the content type, you can't use RewritePath. Instead you can use Response.BinaryWrite or Response.WriteFile to output the file data to the response stream after setting the header.

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

#2


Check out: Create PNG image with C# HttpHandler webservice

检查:使用C#HttpHandler webservice创建PNG图像

It should give you an example of dumping a PNG into the response.

它应该给你一个将PNG转储到响应中的例子。

#3


application/octet-stream will tell your browser that the file is binary and your browser will then download it.

application / octet-stream会告诉您的浏览器该文件是二进制文件,然后您的浏览器将下载该文件。

This has worked for me in the past (in an HttpHandler solely responsible for serving files, hence the clear calls):

这在过去对我有用(在一个HttpHandler中,负责提供文件,因此明确的调用):


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

If you do want to download the file, you can also add:

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


Response.AddHeader("Content-Disposition", "attachment; filename=myimage.png");

to suggest a filename to the browser to save as.

建议浏览器的文件名保存为。

#1


When you use RewritePath, whatever you have written to the head is discarded. Whatever is handling the new path is providing the header.

当您使用RewritePath时,无论您写入头部的任何内容都将被丢弃。无论处理新路径是什么,都提供标题。

If you want to specify the content type, you can't use RewritePath. Instead you can use Response.BinaryWrite or Response.WriteFile to output the file data to the response stream after setting the header.

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

#2


Check out: Create PNG image with C# HttpHandler webservice

检查:使用C#HttpHandler webservice创建PNG图像

It should give you an example of dumping a PNG into the response.

它应该给你一个将PNG转储到响应中的例子。

#3


application/octet-stream will tell your browser that the file is binary and your browser will then download it.

application / octet-stream会告诉您的浏览器该文件是二进制文件,然后您的浏览器将下载该文件。

This has worked for me in the past (in an HttpHandler solely responsible for serving files, hence the clear calls):

这在过去对我有用(在一个HttpHandler中,负责提供文件,因此明确的调用):


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

If you do want to download the file, you can also add:

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


Response.AddHeader("Content-Disposition", "attachment; filename=myimage.png");

to suggest a filename to the browser to save as.

建议浏览器的文件名保存为。