Upload a file to byte array in MySQL successfully, however the content is not readable when download

Tags: , , ,



I am having a problem when downloading the content after I uploaded the file using FormData in JavaScript XMLHttpRequest and connect to Web API and save it to MySQL DB as LONGBLOB data type. When I tried to download the file that is being uploaded previously as BLOB to MySQL DB, the file is being downloaded, however the file cannot be readable anymore. Any solutions?

Here is the code that I am using for uploading the file to the DB as byte array:

  • HTML and Javascript:
<input id="Upload" type="file" accept="application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf" />
let SubmittedData = new FormData();
let XHR = new XMLHttpRequest();

SubmittedData.append("FileContent", $("#Upload").files[0]);

XHR.open("POST", "/UploadFile");
XHR.send(SubmittedData);
XHR.onreadystatechange = function () {
    if (XHR.readyState == 4 && XHR.status == 200)
        alert("Success");
}
  • Web API:
[HttpPost]
public ActionResult UploadFile()
{
    if (Request.Files.Count <= 0)
        return Ok();

    byte[] FileContent = new byte[0];

    using (var reader = new BinaryReader(Request.Files[0].InputStream))
        FileContent = reader.ReadBytes(Request.Files[0].ContentLength);

    InsertToMySQLDB(FileContent);

    return Ok()
}

Here is the code that I am using for retrieve the byte array from DB and download it as PDF (I am using HtmlToPDF library in NuGet for downloading as PDF) and Word:

public ActionResult DownloadPDF()
{
    byte[] FileContent = RetrieveFileContentFromMySQLDB();

    return File(FileContent, "application/pdf", "File.pdf");
}

public ActionResult DownloadWord()
{
    byte[] FileContent = RetrieveFileContentFromMySQLDB();

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", $"attachment;filename=File.doc");
    Response.Charset = string.Empty;
    Response.ContentType = "application/vnd.ms-word";
    Response.Output.Write(Encoding.Default.GetString(FileContent, 0, FileContent.Length));
    Response.Flush();
    Response.End();
    break;
)

EDIT:

There is another problem now, the scenario is:

  • When I upload the PDF file and download it as PDF, it can be downloaded and the content is same like what I have been uploaded before, however when I tried to download it as Word, it is being downloaded, but the content is just all hex characters.

  • When I upload the Word file and download it as PDF, it cannot be downloaded (the file is corrupted), and when I download it as Word, it is being downloaded, but the content is just all hex characters.

Any solutions for the conversion?

Thank you very much

Answer

I think you can change DownloadPDF() to :

public ActionResult DownloadPDF()
{
    byte[] FileContent = RetrieveFileContentFromMySQLDB();

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", $"attachment;filename=File.pdf");
    Response.Charset = string.Empty;
    Response.ContentType = "application/pdf";
    Response.Output.Write(Encoding.Default.GetString(FileContent, 0, FileContent.Length));
    Response.Flush();
    Response.End();
    break;
}

Above is (almost) the same as DownloadWord(). The differences are the headers “Content-Disposition” and “Content-Type”.



Source: stackoverflow