HttpGet return 错误 405
HttpGet return error 405
使用 ASP Web API,我创建了一个获取 ID 然后传送 pdf 文件的方法,然后使用 Google 文档查看器或类似服务来查看文件,
代码看起来像这样,
[HttpGet]
public HttpResponseMessage GetAttachment(string id)
{
try {
string mapping = @"\192.168.3.3\Archieve";
string sourcedir = @"\Digital\";
string filename = id + ".pdf";
string sourceFullPath = mapping + sourcedir + filename;
byte[] dataBytes = new byte[0];
// connect to other network using custom credential
var credential = new NetworkCredential("user", "pass", "192.168.3.3");
using (new NetworkConnection(mapping, credential)) {
dataBytes = File.ReadAllBytes(sourceFullPath);
}
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(dataBytes));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = filename;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
catch (Exception ex) {
return Request.CreateResponse(HttpStatusCode.Gone, ex.Message);
}
}
使用此代码,当我在网络浏览器上打开 link 时,我可以下载 pdf 文件,但是当我尝试使用 Google 文档查看器显示它时,就像这样
https://docs.google.com/viewerng/viewer?url=http://myserver/webapi/api/File/GetAttachment/0317101532
Google未能正确显示文件,
当我使用 https://www.pdfescape.com/open/
等其他服务时,错误是 The remote server returned an error: (405) Method Not Allowed.
编辑: 我认为 Google 文档查看器和 pdfescape 都需要直接 link 到文件,我可以直接生成 link网络 API 控制器?
尝试将文件复制到本地,然后return文件link,类似这样
[HttpGet]
public IHttpActionResult GetAttachment(string id)
{
try {
string mapping = @"\192.168.3.3\Archieve";
string sourcedir = @"\Digital\";
string filename = id + ".pdf";
string sourceFullPath = mapping + sourcedir + filename;
byte[] dataBytes = new byte[0];
// connect to other network using custom credential
var credential = new NetworkCredential("user", "pass", "192.168.3.3");
using (new NetworkConnection(mapping, credential)) {
dataBytes = File.ReadAllBytes(sourceFullPath);
}
// write file to local
string destFullPath = string.Format("{0}/Content/Data//{2}", HttpContext.Current.Server.MapPath("~"), filename);
File.WriteAllBytes(destFullPath, dataBytes);
// return the file name,
return Ok(filename);
// then you can view your docs using Google Viewer like this
// https://docs.google.com/viewer?url=http://[YOUR_SERVER_BASE_URL]/content/data/[FILENAME]
}
catch (Exception ex) {
return Content(HttpStatusCode.PreconditionFailed, ex.Message);
}
}
不要忘记为 'Content' 文件夹添加所需的权限
使用 ASP Web API,我创建了一个获取 ID 然后传送 pdf 文件的方法,然后使用 Google 文档查看器或类似服务来查看文件,
代码看起来像这样,
[HttpGet]
public HttpResponseMessage GetAttachment(string id)
{
try {
string mapping = @"\192.168.3.3\Archieve";
string sourcedir = @"\Digital\";
string filename = id + ".pdf";
string sourceFullPath = mapping + sourcedir + filename;
byte[] dataBytes = new byte[0];
// connect to other network using custom credential
var credential = new NetworkCredential("user", "pass", "192.168.3.3");
using (new NetworkConnection(mapping, credential)) {
dataBytes = File.ReadAllBytes(sourceFullPath);
}
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(dataBytes));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = filename;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
catch (Exception ex) {
return Request.CreateResponse(HttpStatusCode.Gone, ex.Message);
}
}
使用此代码,当我在网络浏览器上打开 link 时,我可以下载 pdf 文件,但是当我尝试使用 Google 文档查看器显示它时,就像这样
https://docs.google.com/viewerng/viewer?url=http://myserver/webapi/api/File/GetAttachment/0317101532
Google未能正确显示文件,
当我使用 https://www.pdfescape.com/open/
等其他服务时,错误是 The remote server returned an error: (405) Method Not Allowed.
编辑: 我认为 Google 文档查看器和 pdfescape 都需要直接 link 到文件,我可以直接生成 link网络 API 控制器?
尝试将文件复制到本地,然后return文件link,类似这样
[HttpGet]
public IHttpActionResult GetAttachment(string id)
{
try {
string mapping = @"\192.168.3.3\Archieve";
string sourcedir = @"\Digital\";
string filename = id + ".pdf";
string sourceFullPath = mapping + sourcedir + filename;
byte[] dataBytes = new byte[0];
// connect to other network using custom credential
var credential = new NetworkCredential("user", "pass", "192.168.3.3");
using (new NetworkConnection(mapping, credential)) {
dataBytes = File.ReadAllBytes(sourceFullPath);
}
// write file to local
string destFullPath = string.Format("{0}/Content/Data//{2}", HttpContext.Current.Server.MapPath("~"), filename);
File.WriteAllBytes(destFullPath, dataBytes);
// return the file name,
return Ok(filename);
// then you can view your docs using Google Viewer like this
// https://docs.google.com/viewer?url=http://[YOUR_SERVER_BASE_URL]/content/data/[FILENAME]
}
catch (Exception ex) {
return Content(HttpStatusCode.PreconditionFailed, ex.Message);
}
}
不要忘记为 'Content' 文件夹添加所需的权限