在C#中,我正在做
context.Response.ContentType = "image/png";
context.RewritePath(sz);
context.Response.ContentType = "image/png";它似乎重定向了路径,因为我可以在我的页面上看到图像。然而,在firefox中,当我右键单击并选择“查看图像”时,我会得到一个下载。使用这段c++代码,我可以看到mime是应用程序/八位字节流
#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;
}我的问题是如何将mime设置为正确的类型?上面我将其硬编码为png格式,但不起作用。然而,我宁愿不硬编码任何mime并让它工作。我能够将XML文件重定向到somefile.aspx,并让页面设置内容类型并生成XML。然而,重写图像路径似乎会让图像正常工作,我只是得不到正确的类型。
如何更正内容/mime类型?作为B计划,我将重写到一个aspx文件。发送图像的最佳方式是什么?我可以做一些像context.Response.OutputStream=stream("filename");一样的事情吗?
发布于 2009-06-22 09:18:14
当您使用RewritePath时,您写入头部的任何内容都将被丢弃。无论处理新路径的是什么,都会提供报头。
如果要指定内容类型,则不能使用RewritePath。相反,您可以在设置header之后使用Response.BinaryWrite或Response.WriteFile将文件数据输出到响应流。
发布于 2009-06-22 09:04:44
查看:Create PNG image with C# HttpHandler webservice
它应该会给出一个将PNG转储到响应中的示例。
https://stackoverflow.com/questions/1026152
复制相似问题