Fileupload com jquery e generic handler asp.net
437 palavras
2 páginas
FileUpload com JQuery e Generic Handler Asp.NetPublicado: 05/09/2012 em ASP.NET, C#, Generic Handler, JQuery
Tags:ASP.NET, C#, fileupload, jquery, upload
0
Olá,
Segue uma forma simples e eficiente de fazer upload de arquivos com JQuery e Asp.Net.
Primeiramente a página Default.aspx:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”UploadJQuery_AspNet.Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
<script src=”http://code.jquery.com/jquery-1.7.2.min.js” type=”text/javascript”></script>
<script src=”jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”jquery-1.4.1.min.js” type=”text/javascript”></script>
<script src=”ajaxupload.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function() { new AjaxUpload(‘#btnCarregar’, { action: ‘UploadHandler.ashx’, onComplete: function(file, response) {
$(“<div>” + response + “</div>”).appendTo(‘#Arquivo’);
$(‘#Status’).html(“Arquivo carregado”);
$(“#btnCarregar”).hide();
}, onSubmit: function(file, ext) { if (!(ext && /^(txt|doc|docx|pdf)$/i .test(ext))) { alert(‘tente novamente usando: .doc, .docx ou .pdf.’); return false;
}
$(‘#Status’).html(“Carregando…”);
$(“#btnCarregar”).show();
}
});
});
</script>
</head>
<body>
<div>
<div id=”Status”>
</div>
<div id=”Arquivo”>
</div>
<input type=”button” id=”btnCarregar” value=”Carregar” />
</div>
</body>
</html>
Agora crie seu generic handler chamado UploadHandler.ashx:
<%@ WebHandler Language=”C#” Class=”UploadHandler” %> using System; using System.Web; using System.Web.SessionState;