6
Guys I managed to send the file through this code:
var jqXHRData = null; $(function () { $('.progress').progress({ percent: 0 }); $("#divUpload").on('click', function () { $('#fu-my-simple-upload').click(); }); $("#hl-start-upload").on('click', function () { if (jqXHRData != null) { if (jqXHRData) { jqXHRData.submit(); } return false; } else { toastr.error("Selecione o arquivo, antes de enviar!", "Mensagem do Sistema"); } }); $("#fu-my-simple-upload").on('change', function () { $("#tbx-file-path").val(this.files[0].name); }); 'use strict'; $('#fu-my-simple-upload').fileupload({ type: 'POST', url: '@Url.Action("IDetailsArchives", "IMaterial")', dataType: 'json', add: function (e, data) { jqXHRData = data }, done: function (event, data) { if (data.result.isUploaded) { $("#tbx-file-path").val("Nenhum Arquivo Selecionado..."); toastr.success(data.result.message, "Mensagem do Sistema"); } else { } jqXHRData = null; var intervalo = window.setInterval(progresso, 3000); }, fail: function (event, data) { if (data.files[0].error) { toastr.error(data.files[0].error, "Mensagem do Sistema"); var intervalo = window.setInterval(progresso, 3000); } jqXHRData = null; } }).on('fileuploadprogressall', function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('.progress').progress({ percent: progress }) }); }); function progresso() { $('.progress').progress({ percent: 0 }); };
Controller:
public virtual ActionResult IDetailsArchives(IArquivoMetadado arq) { HttpPostedFileBase myFile = Request.Files["MyFile"]; bool isUploaded = false; string message = "Arquivo não enviado"; if (myFile != null && myFile.ContentLength != 0) { string pathForSaving = Server.MapPath("~/App_Data/Arquivos"); if (this.CreateFolderIfNeeded(pathForSaving)) { try { string FileNameNew = CriarNome(myFile.FileName, arq.conteudoID, arq.conteudoNome); myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName)); isUploaded = true; message = "Arquivo enviado com sucesso!"; } catch (Exception ex) { message = string.Format("Arquivo não enviado: {0}", ex.Message); } } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); }
View:
<div class="ui grid">
<div class="fifteen wide column centered">
<div class="ui grid">
<div class="six wide column">
<div class="ui fluid basic segment">
<h4>Selecione o Arquivo: </h4>
<div class="ui fluid action input">
<input type="hidden" id="conteudoID" name="conteudoID" value="@ViewBag.ID" />
<input type="hidden" id="conteudoNome" name="conteudoNome" value="@ViewBag.Nome" />
<input type="text" id="tbx-file-path" value="Nenhum Arquivo Selecionado..." readonly="readonly" />
@Html.TextBoxFor(m => m.MyFile, new { id = "fu-my-simple-upload", type = "file" , style = "display: none", accept = ".jpg, .png, .pdf, .doc, .docx" })
<div class="ui blue icon button" id="divUpload">
<i class="folder open outline icon"></i>
</div>
</div>
<br />
<a id="hl-start-upload" class="ui right labeled blue icon button">
<i class="cloud upload icon"></i>
Enviar Arquivo
</a>
<div class="ui green progress">
<div class="bar">
<div class="progress"></div>
</div>
</div>
<h4>
Regras de Envio:<br />
• São aceitos somente os arquivos do formato: JPG, PNG, PDF, DOC e DOCX <br />
• A barra acima mostrar o progresso de envio do arquivo ao servidor.
</h4>
</div>
</div>
<div class="ten wide column">
<div class="fifteen wide column centered">
@Html.Action("IDetailsArchivesLista", "IMaterial", new { id = ViewBag.IDConteudo })
</div>
</div>
</div>
</div>
but how to send other values along with my file? Type id, name and file? The id and name inputs will be Hidden
Update 1 JS Code:
var jqXHRData = null; $(function () { $('.progress').progress({ percent: 0 }); $("#divUpload").on('click', function () { $('#MyFile').click(); }); $("#hl-start-upload").on('click', function () { if (jqXHRData != null) { if (jqXHRData) { $("#fileupload").submit(); } return false; } else { toastr.error("Selecione o arquivo, antes de enviar!", "Mensagem do Sistema"); } }); $("#MyFile").on('change', function () { $("#tbx-file-path").val(this.files[0].name); }); 'use strict'; $('#fileupload').fileupload({ type: 'POST', url: '@Url.Action("IMaterial","IDetailsArchives")', dataType: 'json', add: function (e, data) { jqXHRData = data }, done: function (event, data) { if (data.result.isUploaded) { $("#tbx-file-path").val("Nenhum Arquivo Selecionado..."); toastr.success(data.result.message, "Mensagem do Sistema"); } else { } jqXHRData = null; var intervalo = window.setInterval(progresso, 3000); }, fail: function (event, data) { if (data.files[0].error) { toastr.error(data.files[0].error, "Mensagem do Sistema"); var intervalo = window.setInterval(progresso, 3000); } jqXHRData = null; } }).on('fileuploadprogressall', function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('.progress').progress({ percent: progress }) }); }); function progresso() { $('.progress').progress({ percent: 0 }); };
I advise to look at the code, and adapt it in the @Guilhermenascimento response, because I think you will want to add the progress bar and other information.
– Randrade
Could give an explanation, I personally understood, you used the Formdata, but I think it would be interesting to make it more evident ;)
– Guilherme Nascimento
@Guilhermenascimento Sometimes we do something so natural that we forget. Thanks for remembering. p
– Randrade