简体   繁体   中英

How to send pdf data and save from SQL Server using python(server) and Angular (client)

I have my REST application.

Client side -> Angular 5.

Server side -> python (using cherrypy).

Database -> SQL Server.

Database screen: enter image description here

I want to click button and download a file to my computer. What is the best way to download this file from database?

Python:

with CDN_XL:
        CDN_XL.executeWithParams("""SELECT DAB_Dane FROM CDN.DaneBinarne, CDN.DaneObiekty WHERE DAB_ID = DAO_DABId AND DAB_ID = %s""", (download_id))
        CDN_XL.conn.commit()
        types = CDN_XL.cur.fetchall()
        information_json = []
        if types is not None and len(types) > 0:
            for typ in types:
              information_json.append({"data": str(typ["DAB_Dane"])})

            output_json = {"files":information_json}
            return json.dumps(output_json, ensure_ascii=False)  

I return this to client, but i know this is not correct. It was my first idea.

Angular:

public downloadAttachments(id, idAttachment) {
  const requestUrl = this.firmListBaseUrl + id + '/ZALACZNIKI/' + idAttachment + '/' + idAttachment;
  return this.http.get(requestUrl, { headers: this.sharedServicesService.prepareAuthHeaders() })
  .map(res => {
    return res.json().files;
  });

}

Now I am trying to convert somehow this data to Blob and save as pdf. I guesse is not correct so I decided to write here for help. Thanks!

Looks like you have the data in your db already as binary.

with CDN_XL:
        CDN_XL.executeWithParams("""SELECT DAB_Dane FROM CDN.DaneBinarne, CDN.DaneObiekty WHERE DAB_ID = DAO_DABId AND DAB_ID = %s""", (download_id))
        CDN_XL.conn.commit()
        pdf_bin = CDN_XL.cur.fetchone() 

        cherrypy.response.headers['Content-Type'] = 'application/pdf'
        cherrypy.response.headers['Content-Disposition'] = 'inline;filename="report.pdf"'
        return pdf_bin["DAB_Dane"]

Ps. I'm a django guy. My cherrypi-foo is not very strong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM