View
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Scanner</title>
<script type="text/javascript" src="Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="Scripts/jtwain.js"></script>
</head>
<body>
<div id="notify" style="padding: 5px; font-family: Verdana; font-size: 9px">.</div>
<div id="toolstrip">
<select id="ds" name="D1" style="width:300px; margin:2px;">
<option>---</option>
</select>
<input id="acquire" type="button" value="Acquire" />
<input id="upload" type="button" value="Upload" />
</div>
<div id="picturebox" style="padding-top: 5px; padding-bottom: 5px">
<img id="image" name="nameImagem" src="" alt="Acquired image" style="height: 480px" />
</div>
</body>
UploadHandler.ashx
public void ProcessRequest(HttpContext context) {
try {
if(context.Request.HttpMethod=="POST") {
var _args = context.Request.Params;
switch(_args["action"].ToLower()) {
case "create":
context.Response.Write(this._Create(_args["ext"]));
break;
case "append":
this._Append(_args["name"],Convert.FromBase64String(_args["data"]));
break;
}
return;
}
}catch(Exception ex) {
context.Response.StatusCode=400;
context.Response.StatusDescription=ex.Message;
var _msg = string.Empty;
for(var _ex = ex; _ex!=null; _ex=_ex.InnerException) {
_msg+=string.Format("{1}: {2}{0}{3}{0}{0}",Environment.NewLine,ex.GetType().Name,ex.Message,ex.StackTrace);
}
context.Response.Write(_msg);
}
}
private string _Create(string ext) {
return Path.GetFileName(Path.ChangeExtension(Path.GetTempFileName(),ext));
}
private void _Append(string name,byte[] data) {
using(var _stream = File.Open(Path.Combine(HostingEnvironment.MapPath("~/Documentos"),Path.GetFileName(name)),FileMode.Append)) {
_stream.Write(data,0,data.Length);
}
}
public bool IsReusable {
get {
return false;
}
}
Controller
[HttpPost]
public ActionResult AdicionaDocumento(Documento documento, int id)
{
int idInstituicao = Convert.ToInt32(Session["IdInstituicaoLogada"]);
try
{
if (documento != null && ModelState.IsValid)
{
Documento doc = new Documento();
doc.IdAluno = id;
doc.IdEntidade = 1;
doc.IdInstituicao = idInstituicao;
doc.IdUsuario = 1;
doc.IdTipoDocumento = documento.IdTipoDocumento;
doc.IdTemporalidade = documento.IdTemporalidade;
doc.IdLocalFisico = documento.IdLocalFisico;
doc.NomeDocumento = "";
doc.DescricaoDocumento = "";
doc.ObservacaoDocumento = documento.ObservacaoDocumento;
doc.DocumentoUrl = documento.DocumentoUrl;
doc.DataDocumento = documento.DataDocumento;
doc.DataRecebimento = documento.DataRecebimento;
unitOfWork.DocumentoGenericRepository.Adiciona(doc);
unitOfWork.Salva();
}
return Json(new[] { documento }.ToDataSourceResult(new DataSourceRequest(), ModelState));
}
catch (System.Data.Entity.Core.EntitySqlException)
{
ModelState.AddModelError("", "Erro EntitySqlException.");
return View();
}
catch (System.Data.Entity.Core.EntityCommandExecutionException)
{
ModelState.AddModelError("", "Erro EntityCommandExecutionException.");
return View();
}
finally
{
if (unitOfWork.DocumentoGenericRepository != null) unitOfWork.DocumentoGenericRepository.Dispose();
}
}