Search code examples
c#pdfmodel-view-controlleracrobat

Retrieve submitted PDF form on server


I created a PDF Form with a submit button which sends it to my website, that processes it and stores it for later usage.

On my website, i would like to take the POSTed pdf, and save it. However, I can't find how to convert to incoming stream back to a PDF.

This is the code i currently have

[HttpPost]
public string PostPDF()
{
    using (var sr = new System.IO.StreamReader(Request.InputStream))
    {
        var fdfStream = sr.ReadToEnd();
    }

    ...
    return "Form submitted successfully!";
}

other things i tried were receiving an httpPostedFileBase and a byte[], like so :

[HttpPost]
public ActionResult PostPDF(HttpPostedFileBase file)

[HttpPost]
public ActionResult PostPDF(byte[] file)

both of which were empty results.


Solution

  • The code below should do the job.

            HttpRequestMessage request = this.Request;
            if (!request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var parts = await request.Content.ReadAsMultipartAsync();
            Stream inputStream = null;
            string fname = null;
    
            foreach (var part in parts.Contents)
            {
                string dispoName = part.Headers.ContentDisposition.Name;
                if (dispoName.Equals("\"inputFile\""))
                {
                    fname = part.Headers.ContentDisposition.FileName;
                    inputStream = await part.ReadAsStreamAsync();
                }
            }
    
            if (inputStream == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }