欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

railsAPI接收Base64文件

程序员文章站 2022-07-12 20:18:39
...
             tmp_dir = "tmp/pdf/"
              FileUtils.mkdir(tmp_dir) unless File.exists?(tmp_dir)
              pdf_tmp_path = "#{tmp_dir}#{file_name}.pdf"
              File.open(pdf_tmp_path, 'wb') { |f| f.write(Base64.decode64(pdf_base64_code)) }
              attachment = Attachment.new(attachment_entity_type: finance_bill.class.name, attachment_entity_id: finance_bill.id)
              File.open(pdf_tmp_path) do |f|
                attachment.path = f
              end
              attachment.save



# 导出txt价格
  def pubchem_txt
    head = %w{smiles PUBCHEM_EXT_DATASOURCE_REGID  PUBCHEM_SUBSTANCE_SYNONYM  PUBCHEM_EXT_SUBSTANCE_URL  PUBCHEM_EXT_DATASOURCE_URL PUBCHEM_GENERIC _REGISTRY_NAME}
    local_file_d = File.join("") 

    local_file_d << head.join("\t") + "\n" 

    chemical_ids = StockChemicalPriceRecord.select(:chemical_id).joins("left join ban_chemicals b on b.chemical_id = stock_chemical_price_records.chemical_id ").where('b.id is null ').group(:chemical_id)
    chemicals = Chemical.where(id:chemical_ids)
    chemicals.each do |chemical|
      data_list = ["#{chemical.smiles}","#{chemical.catalog_no}","#{chemical.name}","http://www.ichemical.com/products/#{chemical.cas}.html","http://www.ichemical.com","#{chemical.cas}"]
      local_file_d << data_list.join("\t") + "\n"
    end

    send_data(
        local_file_d,
        filename: "pubchem Package Info-#{rand(99999)}.txt",
        type: "application/txt",
        disposition: "attachment"
    )
  end