Decoding a base64 file in Javascript / jQuery to download

Today I experimented with binary SQL objects. I started by storing the image in a table by making an AJAX request to encode a base64 image and displaying it with.

<img src="data:image/jpeg;base64,' + base64imageReturnedWithAjax + '">')

The image is displayed normally.

The web project I'm working on also requires downloading files (mostly PDF). Great, I thought I would save the PDF as a binary SQL object, also collect it from the server in the same way, and then somehow magically decode it for download at the other end.

Help!

At first I tried to decode it using the base64 jQuery decoder (https://github.com/carlo/jquery-base64) with:

$.base64.decode(base64fileReturnedWithAjax)

This causes the following error in the console:

Uncaught Cannot decode base64
_getbyte64
_decode
$.ajax.success
f.Callbacks.o
f.Callbacks.p.fireWith
w
f.support.ajax.f.ajaxTransport.send.d

, : ? , ! , SQL?

, ATfPT


EDIT: , -, db/server ( VB.NET). Fileblob - .

    'Having connected to the table
    While lrd.Read()
        Dim fileBytes = CType(lrd("Fileblob"), Byte())
        Dim stream = New MemoryStream(fileBytes, 0, fileBytes.Length)
        Dim base64String = Convert.ToBase64String(stream.ToArray())
        document.Add(base64String)
    End While

    Dim serializer As New JavaScriptSerializer()
    Dim returnVal As String = serializer.Serialize(document)

    Return returnVal
+3
2

Inline base 64 ( ), , , . , , PDF (varbinary(max) SQL Server).

, HTTP (Controller, ASHX, ASPX ..), HTTP , SQL Server, Response.BinaryWrite() . , . .

, 64- , 64- . , / ( ).

, HTTP . 64, JSON, ( ).

HTTP

, PDF . .

Generic Handler Visual Studio. ASHX.

public partial class RequestHandler : IHttpHandler
{
    public void ProcessRequest( HttpContext context ) {
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        byte[] bytes = null; // get from your database
        string fileName = null; // get from database or put something generic here like "file.pdf"

        response.ContentType = "application/pdf";
        response.AddHeader( "content-disposition", "inline;filename=" + fileName );
        response.AddHeader( "Content-Length", bytes.Length.ToString() );

        response.Buffer = true;
        response.BinaryWrite( bytes );
        response.Flush();
    }

    public bool IsReusable {
        get {
            return true;
        }
    }
}

, , . <a href="RequestHandler.ashx?id=abc">Download</a>. ASPX , ..

+4

, , , javascript . , ( ) http- .

( ):

  • object PDF . jquery. URL- URI data:.

  • ( ), window.location.assign('data:xxx), PDF, , , PDF .

, , ajax - download , , , .

0

All Articles