Karuta’s ASP & M$ SQLserver

Dicas, códigos e outras coisinhas de meus estudos para não esquecer…

Archive for the ‘Jquery’ Category

Send file data via JSON from REST API and open as download file in browser (with Classic ASP/JQuery)

Posted by karuta em junho 7, 2017

Pet project challenge of the day. I have a REST API that delivers a JSON response, and I want it to include file data, so the browser at the client end can download the file. In real world what I’m doing is offering a link to the user to export some data to an XLS and download to their desktop.

I messed around with this for a while. Carriage of the file data seemed simple – at the REST end encode it to base64 and stick that into the JSON. The problem lies at the client end. I have JQuery calling the REST, and dealing with the response. How then to get the base64 out of the JSON and contrive to offer it as a File Save As situation?

Finally here’s what I came up with, it was sparked by something I saw on StackOverflow, although I cannot lay my hands on the post right now:

At the REST, encode the XLS file text to base64, and return the base64 in the JSON as a text element

On the client, I have an ASP script that acts as the controller, it receives requests from the pages (eg JQuery calls this page), then it calls the remote REST, and returns the response back to JQuery. Obviously cross-browser rules means my JQuery can only call URLs on the same domain, and my REST is not. So this script acts as the transporter for the calls. It’s even called Frank.

On the page I have a form with a hidden field, JQuery puts the base64 file data into that hidden field, then submits the form to another page.

1
2
3
4
<form id="base64Form" action="openFileToBrowser.asp" method="post" name="base64Form">
<input id="base64data" type="hidden" name="base64data" value="" />
<input id="fileName" type="hidden" name="fileName" value="responses.xls" />
</form>

This new page grabs the file data, decodes it from base64 back to text, sets the content headers to XLS etc. And hey presto, the user gets a File Save As dialog. Here’s the code on that page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit%>
<% response.expires = 0 %>
<!--#include virtual="/includes/hex_sha1_base64.asp" -->
<%
    dim base64data
    base64data=request.form("base64data")
    if base64data<>"" then
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment; filename="&request.form("fileName")
    response.write Base64_Decode(base64data)
    response.Flush()
    end if
%>

The base64 encode/decode routine I’m using is part of the very handy hex_sha1_base64.asp file which I also use for other encryption and encoding tasks (for example I use it for encryption as part of talking to OAUTH based APIs).

I haven’t tested this in all browsers yet, be interesting to see if kicks up problems with IE etc.

Anúncios

Posted in asp, Jquery | 1 Comment »

Acentuação para conteúdos carregados por AJAX

Posted by karuta em outubro 1, 2012

  • Em ColdFusion:
<cfcontent type="text/html; charset=ISO-8859-1">
  • Em ASP:
<% Response.Charset="ISO-8859-1" %>
  • Em PHP:
<?php header("Content-Type: text/html;  charset=ISO-8859-1",true); ?>
  • Em JSP:
<%@ page contentType="text/html; charset=ISO-8859-1" %>

Posted in javascript, Jquery | Leave a Comment »

Enviar formulário pelo FANCYBOX

Posted by karuta em maio 3, 2011

$(“#login_form”).bind(“submit”, function() {

$.ajax({
type        : “POST”,
cache       : false,
url         : “/login.php”,
data        : $(this).serializeArray(),
success: function(data) {
$.fancybox(data);
}
});

return false;

});

<form action=”/login.php” method=”POST” id=”login_form”>
<input type=”input” size=”20″ name=”username” />
<input type=”submit” value=”Login” />
</form>

Posted in Jquery | Leave a Comment »

Jquery – Desabilitar SUBMIT quando usar o ENTER nos formulários

Posted by karuta em junho 23, 2010

if(event.preventDefault) event.preventDefault();

* ver pedidos_novo_auto.asp (spon 2010)

Posted in Jquery | Leave a Comment »

jQuery load() problemas no IE – event.preventDefault() function

Posted by karuta em maio 28, 2010

No caso do código abaixo:

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html");
    $(".islice").show('fast');  
    e.preventDefault();
});



no IE, você pode usar
 event.returnValue = false; 

para atingir o mesmo resultado.

E para não receber um erro, você pode testar para a existência de preventDefault:

 if(event.preventDefault) event.preventDefault(); 
(usei no clientes.asp do novo SPon e deu certo no IE)

Posted in Jquery | Leave a Comment »