locked
IIS unable to download pdf RRS feed

  • Question

  • User-1821720307 posted

    I am hosting a web api on IIS 10.0, I created a method that will retrieve a pdf from a folder. Works well locally in VS and works when the files are on the server, but when I am trying to retrieve the files from a network file system I am getting and error and in IIS log it is 404 0 0 15. Now i Checked that 15 is "drive not found" error. I think I need to give permission to some user on IIS to this folder. I am unable to determine which user needs access to the network folder. Any help appreciated.

    Tuesday, July 24, 2018 7:42 PM

All replies

  • User753101303 posted

    Hi,

    It is done with code? A server side runtime error should be http 500. If done using IIS configuration could it be that you mapped a virtual folder to a drive letter. Use UNC path instead (it works if allowed while drive letters are configured for each user profile).

    Tuesday, July 24, 2018 7:52 PM
  • User-1821720307 posted

    Thanks for your response below is my code

    the fullpdfpath  in the program is \\server\all invoices\ in  web.config file.  Its failing --if (!File.Exists(fullpdfpath)). I do not know what I am doing wrong.

    --code starts here-------------------------------------

    string fileName = invoiceName + ".pdf";


    string filepath = ConfigurationManager.AppSettings["InvoicePath"].ToString();

    string fullpdfpath = @filepath + fileName;
    //Failing here
    if (!File.Exists(fullpdfpath))
    {
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, fullpdfpath + "File Not Found");
    //throw new HttpResponseException(HttpStatusCode.NotFound);
    }

    //Copy the source file stream to MemoryStream and close the file stream
    MemoryStream responseStream = new MemoryStream();
    Stream fileStream = File.Open(fullpdfpath, FileMode.Open);

    fileStream.CopyTo(responseStream);
    fileStream.Close();
    responseStream.Position = 0;

    HttpResponseMessage response = new HttpResponseMessage();
    response.StatusCode = HttpStatusCode.OK;

    //Write the memory stream to HttpResponseMessage content
    response.Content = new StreamContent(responseStream);
    string contentDisposition = string.Concat("attachment; filename=", fileName);
    response.Content.Headers.ContentDisposition =
    ContentDispositionHeaderValue.Parse(contentDisposition);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    return response;

    Tuesday, July 24, 2018 9:44 PM
  • User283571144 posted

    Hi apidude,

    According to your description, I think you may not use the right file path.

    So the the web application couldn't locate the right path from the server.

    Could you please tell me is the pdf file folder inside the web application??

    If the pdf folder is inside the web application, I suggest you could use server.mappath to access that path.

    https://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx  

    Best Regards,

    Brando

    Wednesday, July 25, 2018 8:49 AM
  • User753101303 posted

    Ok. I better understand why you have a 404.

    Double check fullpdfpath is correct and for now I would comment the test to see which exception happens when I actually try to read the file. The account under which your web app runs needs to have access (but it's likyl best to see first what actually happens when trying to read this file).

    Wednesday, July 25, 2018 9:01 AM