Search code examples
c#asp.netasp.net-web-apigoogle-document-viewer

HttpGet return error 405


Using ASP Web API, I create a method that takes an ID then deliver a pdf file, then using Google docs viewer, or similar service, to view the file,

The code looks something like this,

[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);
    }
}

With this code, I'm able to download the pdf file when I open the link on web browser, but when I try to display it using Google docs viewer, like this

https://docs.google.com/viewerng/viewer?url=http://myserver/webapi/api/File/GetAttachment/0317101532

Google failed to display the file without error,

And when I use other service like https://www.pdfescape.com/open/ the error is The remote server returned an error: (405) Method Not Allowed.

EDIT: I think both Google Docs viewer and pdfescape need direct link to the file, can I generate direct link on Web API controller?


Solution

  • Try to copy the file to local, and then return the file link, something like this

    [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);
        }
    }
    

    Don't forget to add required permission on 'Content' folder