Downloading a stored PDF gets damaged












2















I'm working in a Web in .Net Core with MVC. I want to download a PDF from my database in SQL Server. I save the PDF in my database in varbinary, and in the controller, I get the PDF in byte.



But when I download the PDF from my web I want to see the PDF. Said the PDF is damaged.



This is the method to download the PDF in the controller:



[HttpGet]
public FileContentResult DownloadFile(string DNI)
{
Byte file1 = _manager.SetFile1(DNI);

Response.Headers.Add("content-disposition", "attachment; filename=dddddd.pdf");
return new FileContentResult(file1,"application/pdf");
}


This is the code when i insert the PDF to the controller, and after i save in the SQL Server with varbinary type:



var reader2 = new StreamReader(certificateCompany.OpenReadStream());

string contentAsString2 = reader2.ReadToEnd();

byte contentAsByteArray2 = GetBytes(contentAsString2);


certificateCompany is a IFormFile type. He come from here:



<input type="file" name="attachedWorking1" id="attachedWorking1" accept=".pdf" multiple />


After,in my controller:



        [HttpPost]
public async Task<IActionResult> FormCause(IFormFile attachedWorking1)
{
var certificateCompany = attachedWorking1;

var reader1 = new StreamReader(certificateCompany.OpenReadStream());

string contentAsString1 = reader1.ReadToEnd();

byte contentAsByteArray1 = GetBytes(contentAsString1);

petition.file1 = contentAsByteArray1;

_manager.InsertPetition(petition);

return View("Close");
}









share|improve this question





























    2















    I'm working in a Web in .Net Core with MVC. I want to download a PDF from my database in SQL Server. I save the PDF in my database in varbinary, and in the controller, I get the PDF in byte.



    But when I download the PDF from my web I want to see the PDF. Said the PDF is damaged.



    This is the method to download the PDF in the controller:



    [HttpGet]
    public FileContentResult DownloadFile(string DNI)
    {
    Byte file1 = _manager.SetFile1(DNI);

    Response.Headers.Add("content-disposition", "attachment; filename=dddddd.pdf");
    return new FileContentResult(file1,"application/pdf");
    }


    This is the code when i insert the PDF to the controller, and after i save in the SQL Server with varbinary type:



    var reader2 = new StreamReader(certificateCompany.OpenReadStream());

    string contentAsString2 = reader2.ReadToEnd();

    byte contentAsByteArray2 = GetBytes(contentAsString2);


    certificateCompany is a IFormFile type. He come from here:



    <input type="file" name="attachedWorking1" id="attachedWorking1" accept=".pdf" multiple />


    After,in my controller:



            [HttpPost]
    public async Task<IActionResult> FormCause(IFormFile attachedWorking1)
    {
    var certificateCompany = attachedWorking1;

    var reader1 = new StreamReader(certificateCompany.OpenReadStream());

    string contentAsString1 = reader1.ReadToEnd();

    byte contentAsByteArray1 = GetBytes(contentAsString1);

    petition.file1 = contentAsByteArray1;

    _manager.InsertPetition(petition);

    return View("Close");
    }









    share|improve this question



























      2












      2








      2


      0






      I'm working in a Web in .Net Core with MVC. I want to download a PDF from my database in SQL Server. I save the PDF in my database in varbinary, and in the controller, I get the PDF in byte.



      But when I download the PDF from my web I want to see the PDF. Said the PDF is damaged.



      This is the method to download the PDF in the controller:



      [HttpGet]
      public FileContentResult DownloadFile(string DNI)
      {
      Byte file1 = _manager.SetFile1(DNI);

      Response.Headers.Add("content-disposition", "attachment; filename=dddddd.pdf");
      return new FileContentResult(file1,"application/pdf");
      }


      This is the code when i insert the PDF to the controller, and after i save in the SQL Server with varbinary type:



      var reader2 = new StreamReader(certificateCompany.OpenReadStream());

      string contentAsString2 = reader2.ReadToEnd();

      byte contentAsByteArray2 = GetBytes(contentAsString2);


      certificateCompany is a IFormFile type. He come from here:



      <input type="file" name="attachedWorking1" id="attachedWorking1" accept=".pdf" multiple />


      After,in my controller:



              [HttpPost]
      public async Task<IActionResult> FormCause(IFormFile attachedWorking1)
      {
      var certificateCompany = attachedWorking1;

      var reader1 = new StreamReader(certificateCompany.OpenReadStream());

      string contentAsString1 = reader1.ReadToEnd();

      byte contentAsByteArray1 = GetBytes(contentAsString1);

      petition.file1 = contentAsByteArray1;

      _manager.InsertPetition(petition);

      return View("Close");
      }









      share|improve this question
















      I'm working in a Web in .Net Core with MVC. I want to download a PDF from my database in SQL Server. I save the PDF in my database in varbinary, and in the controller, I get the PDF in byte.



      But when I download the PDF from my web I want to see the PDF. Said the PDF is damaged.



      This is the method to download the PDF in the controller:



      [HttpGet]
      public FileContentResult DownloadFile(string DNI)
      {
      Byte file1 = _manager.SetFile1(DNI);

      Response.Headers.Add("content-disposition", "attachment; filename=dddddd.pdf");
      return new FileContentResult(file1,"application/pdf");
      }


      This is the code when i insert the PDF to the controller, and after i save in the SQL Server with varbinary type:



      var reader2 = new StreamReader(certificateCompany.OpenReadStream());

      string contentAsString2 = reader2.ReadToEnd();

      byte contentAsByteArray2 = GetBytes(contentAsString2);


      certificateCompany is a IFormFile type. He come from here:



      <input type="file" name="attachedWorking1" id="attachedWorking1" accept=".pdf" multiple />


      After,in my controller:



              [HttpPost]
      public async Task<IActionResult> FormCause(IFormFile attachedWorking1)
      {
      var certificateCompany = attachedWorking1;

      var reader1 = new StreamReader(certificateCompany.OpenReadStream());

      string contentAsString1 = reader1.ReadToEnd();

      byte contentAsByteArray1 = GetBytes(contentAsString1);

      petition.file1 = contentAsByteArray1;

      _manager.InsertPetition(petition);

      return View("Close");
      }






      c# asp.net-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 21 at 18:32







      Daniel Ahumada

















      asked Jan 20 at 14:21









      Daniel AhumadaDaniel Ahumada

      164




      164
























          1 Answer
          1






          active

          oldest

          votes


















          4














          This is an XY problem because the code saving the PDF to the database is incorrect.



          You are saving a string not a PDF.



          Do not use a stream reader in this case. It is for text.



          Instead Read the bytes directly from the stream returned when certificateCompany.OpenReadStream() is called.



          [HttpPost]
          public async Task<IActionResult> FormCause(IFormFile attachment) {
          var certificateCompany = attachment;

          var stream = certificateCompany.OpenReadStream();

          var length = (int)stream.Length;

          byte data = new byte[length];

          await stream.ReadAsync(buffer: data, offset: 0, count: length);

          //...

          petition.file1 = data;

          _manager.InsertPetition(petition);

          return View("Close");
          }





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277388%2fdownloading-a-stored-pdf-gets-damaged%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            This is an XY problem because the code saving the PDF to the database is incorrect.



            You are saving a string not a PDF.



            Do not use a stream reader in this case. It is for text.



            Instead Read the bytes directly from the stream returned when certificateCompany.OpenReadStream() is called.



            [HttpPost]
            public async Task<IActionResult> FormCause(IFormFile attachment) {
            var certificateCompany = attachment;

            var stream = certificateCompany.OpenReadStream();

            var length = (int)stream.Length;

            byte data = new byte[length];

            await stream.ReadAsync(buffer: data, offset: 0, count: length);

            //...

            petition.file1 = data;

            _manager.InsertPetition(petition);

            return View("Close");
            }





            share|improve this answer






























              4














              This is an XY problem because the code saving the PDF to the database is incorrect.



              You are saving a string not a PDF.



              Do not use a stream reader in this case. It is for text.



              Instead Read the bytes directly from the stream returned when certificateCompany.OpenReadStream() is called.



              [HttpPost]
              public async Task<IActionResult> FormCause(IFormFile attachment) {
              var certificateCompany = attachment;

              var stream = certificateCompany.OpenReadStream();

              var length = (int)stream.Length;

              byte data = new byte[length];

              await stream.ReadAsync(buffer: data, offset: 0, count: length);

              //...

              petition.file1 = data;

              _manager.InsertPetition(petition);

              return View("Close");
              }





              share|improve this answer




























                4












                4








                4







                This is an XY problem because the code saving the PDF to the database is incorrect.



                You are saving a string not a PDF.



                Do not use a stream reader in this case. It is for text.



                Instead Read the bytes directly from the stream returned when certificateCompany.OpenReadStream() is called.



                [HttpPost]
                public async Task<IActionResult> FormCause(IFormFile attachment) {
                var certificateCompany = attachment;

                var stream = certificateCompany.OpenReadStream();

                var length = (int)stream.Length;

                byte data = new byte[length];

                await stream.ReadAsync(buffer: data, offset: 0, count: length);

                //...

                petition.file1 = data;

                _manager.InsertPetition(petition);

                return View("Close");
                }





                share|improve this answer















                This is an XY problem because the code saving the PDF to the database is incorrect.



                You are saving a string not a PDF.



                Do not use a stream reader in this case. It is for text.



                Instead Read the bytes directly from the stream returned when certificateCompany.OpenReadStream() is called.



                [HttpPost]
                public async Task<IActionResult> FormCause(IFormFile attachment) {
                var certificateCompany = attachment;

                var stream = certificateCompany.OpenReadStream();

                var length = (int)stream.Length;

                byte data = new byte[length];

                await stream.ReadAsync(buffer: data, offset: 0, count: length);

                //...

                petition.file1 = data;

                _manager.InsertPetition(petition);

                return View("Close");
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 21 at 20:07

























                answered Jan 20 at 14:47









                NkosiNkosi

                115k16127192




                115k16127192
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277388%2fdownloading-a-stored-pdf-gets-damaged%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage