Reputation: 103
I'm trying to download an file what can be pdf, cvs, word, jpg or png. To do this, I'm using angularjs + filesaver.js. don't know why but, my downloads are always corrupted.
Someone can help me ?
Note: midia.Nome - Name of my file. midia.Tipo - The type of the file. midia.Path - Physical path on project. midia.MidiaBytesString - Bytes on Base64 to convert to blob.
<a href="{{midia.path}}" target="_blank" ng-click="Download(midia.MidiaBytesString, midia.Nome, midia.Tipo)">Download</a> <br />
My File download method:
$scope.Download = function(bytes,nome,tipo){
var file = new File([bytes], nome , {type: "application/octet-stream"});
saveAs(file);
};
My View Model
public class MidiaViewModel { public int MidiaId { get; set; }
public string MidiaDescricao { get; set; }
//public string MidiaPath { get; set; }
public byte[] MidiaBytes { get; set; }
public string MidiaBytesString { get; set; }
public int OsId { get; set; }
public virtual OsViewModel Os { get; set; }
public string MidiaNome { get; set; }
public string MidiaType { get; set; }
public string MidiaPath { get; set; }
public long MidiaSize { get; set; }
}
My register Midia:
[HttpPost]
public JsonResult AtualizarMidias(IEnumerable<HttpPostedFileBase> arquivos, IEnumerable<string> descricoes, int osId)
{
var ordermDeServico = _osService.ObterPorIdLazyLoad(osId);
foreach (var file in arquivos)
{
string targetFolder = HttpContext.Server.MapPath("~/Content/uploads/");
string targetPath = Path.Combine(targetFolder, file.FileName);
if (file != null && file.ContentLength > 0)
{
var itens = arquivos.ToList();
var des = descricoes.ToList();
var index = itens.FindIndex(c => c.FileName == file.FileName);
var midiaViewModel = new MidiaViewModel
{
MidiaType = file.ContentType,
MidiaNome = file.FileName,
MidiaPath = targetPath,
MidiaSize = file.ContentLength,
MidiaDescricao = des[index].ToString(),
MidiaBytes = converterArquivo(file)
};
var midia = Mapper.Map<MidiaViewModel, Midia>(midiaViewModel);
midia.OsId = ordermDeServico.OSId;
file.SaveAs(targetPath);
_midiaService.Salvar(midia);
}
}
return Json(new { resultado = true }, JsonRequestBehavior.AllowGet);
}
And where i make the call to my angular $scope:
[HttpPost]
public string getMidiasOS(int idOS)
{
var getMidias = _midiaService.ObterMidiaPorIdOs(idOS);
List<MidiaAngularViewModel> midiaNova = new List<MidiaAngularViewModel>();
foreach (var midia in getMidias)
{
midiaNova.Add(new MidiaAngularViewModel
{
Id = midia.MidiaId,
Path = midia.MidiaPath,
Tipo = midia.MidiaType,
Descricao = midia.MidiaDescricao,
MidiaBytesString = Convert.ToBase64String(midia.MidiaBytes),
Nome = midia.MidiaNome
});
}
return JsonConvert.SerializeObject(midiaNova);
}
EDIT
Guys, I solved my problem using the Ameni topic in the follow link: download file using angularJS and asp.net mvc
All I need to do was create like the Post method on the Ameni topic and the download by path function.
Upvotes: 1
Views: 1669
Reputation: 18402
Simply add/use the download
attribute and you will be fine. More about download attribute can be found at w3c download documentation.
<a href="{{midia.path}}" download="filename">Download</a>
Upvotes: 1