0

I want to open a file (it may be any type of file like .txt, .doc, .pdf or an image..) that is stored to the database in blob format.

I execute an ajax call to get the blob response:

jQuery

$.get("ajax.aspx?t=getExpAttachedFile&expFileId=" + id, function (data) {
    console.log(data);
    if (data) {
        var arr = [];
        arr.push(data);
        //Not able to get the file type here..
        var oMyBlob = new Blob(arr, { type: '...' });
        var url = URL.createObjectURL(oMyBlob);
        window.open(oMyBlob);
    } else {
        // no data
    }
});

C# (getting the stored blob file from db)

private void getExpAttachedFile() {
    SessionProvider sp = (SessionProvider)Session[Session.SessionID];
    eDbs.DbeStudio dc = new DbeStudio( sp.ConnString );
    var data = Request["expFileId"];
    try {
        var fileBlob = dc.SS_Aplication_FileBlobs.FirstOrDefault ( p=>p.ID == Convert.ToInt32(data));
        if (fileBlob != null) {
                Response.Write( fileBlob.Blob );
            }       
    }
    catch {
        Response.Write( "0" );
    }
}

The data contain the blob string, but I am unable to transform in to the initial file.

kapantzak
  • 11,610
  • 4
  • 39
  • 61
  • If i were you ; i will create a file which is created by blob in c# and send filePath to jquery. – hurricane Sep 10 '15 at 12:02
  • Why use AJAX for this, that just complicates things. `window.location.href="ajax.aspx?t=getExpAttachedFile&expFileId=123"`, and you’re done … – CBroe Sep 10 '15 at 12:21
  • Similar http://stackoverflow.com/questions/17657184/using-jquerys-ajax-method-to-retrieve-images-as-a-blob/17682424#17682424 – Musa Sep 15 '15 at 17:20

0 Answers0