我在ASP.Net核心中创建了Wep API以返回PDF.这是我的代码:
public HttpResponseMessage Get(int id)
{
var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
var stream = new System.IO.FileStream(@"C:\Users\shoba_eswar\Documents\REquest.pdf", System.IO.FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "NewTab";
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
return response;
}
Run Code Online (Sandbox Code Playgroud)
但它只返回JSON响应:
{
"version":{
"major":1,
"minor":1,
"build":-1,
"revision":-1,
"majorRevision":-1,
"minorRevision":-1
},
"content":{
"headers":[
{
"key":"Content-Disposition",
"value":[
"attachment; filename=NewTab"
]
},
{
"key":"Content-Type",
"value":[
"application/pdf"
]
}
]
},
"statusCode":200,
"reasonPhrase":"OK",
"headers":[
],
"requestMessage":null,
"isSuccessStatusCode":true
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了吗?
我目前正在尝试在ASP.NET 4中做一些简单而直接的事情,但是现在在ASP.NET 5中并不是这样.
以前使用UrlHelper它很简单:
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Run Code Online (Sandbox Code Playgroud)
但是我不能为我的生活包围我如何使用新的UrlHelper.我正在看测试用例,要么我完全愚蠢,要么我错过了一些东西,我似乎无法弄明白.这里有任何帮助来清理这个都很棒.
我写了下面的代码,用于向我的机器人发送照片,但在我的信息流中,我有两个例外的读写,我的照片没有发送.
我想也许原因是这个错误,但我无法解决它:
stream.ReadTimeout
抛出了'System.InvalidOperationException'类型的异常
using (var stream = System.IO.File.Open("a.jpg", FileMode.Open))
{
var fileToSend = new FileToSend("a.jpg", stream);
Task.Run(() => bot.SendPhotoAsync(u.Message.Chat.Id, fileToSend).ConfigureAwait(false));
}
Run Code Online (Sandbox Code Playgroud) 从来没有这样做过,所以不确定它涉及到什么.我做了搜索并找到了很多答案,但它们比我需要的更复杂.例如,他们需要放大,生成,创建准确的缩略图,在网页中嵌入实际的PDF等...但我的问题更简单: 如果我在网页上显示他的信息的人有一些PDF显示我只想在页面上放置一个通用的PDF图标,人们点击它,实际的PDF将在浏览器的新标签页中打开.
这样做涉及什么? 它不像文件路径,PDF在SQL Server中保存为二进制对象,或者它在SQL Server中保存它的任何内容.它不是服务器上的文件磁盘路径