使用 AngularJS + FileSaver.js + .Net MVC 下载文件
Download a file with AngularJS + FileSaver.js + .Net MVC
我正在尝试下载一个文件,可以是 pdf、cvs、word、jpg 或 png。为此,我使用 angularjs + filesaver.js。不知道为什么,我的下载总是损坏。
有人可以帮助我吗?
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 />
我的文件下载方法:
$scope.Download = function(bytes,nome,tipo){
var file = new File([bytes], nome , {type: "application/octet-stream"});
saveAs(file);
};
我的视图模型
public class MidiaViewModel
{
public int MidiaId { 得到;放; }
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; }
}
我注册的 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);
}
我在哪里调用我的 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);
}
编辑
伙计们,我使用以下 link 中的 Ameni 主题解决了我的问题:download file using angularJS and asp.net mvc
我需要做的就是像 Ameni 主题上的 Post 方法和按路径下载功能一样创建。
只需 add/use download
属性即可。有关下载属性的更多信息,请参见 w3c download documentation。
<a href="{{midia.path}}" download="filename">Download</a>
我正在尝试下载一个文件,可以是 pdf、cvs、word、jpg 或 png。为此,我使用 angularjs + filesaver.js。不知道为什么,我的下载总是损坏。
有人可以帮助我吗?
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 />
我的文件下载方法:
$scope.Download = function(bytes,nome,tipo){
var file = new File([bytes], nome , {type: "application/octet-stream"});
saveAs(file);
};
我的视图模型
public class MidiaViewModel { public int MidiaId { 得到;放; }
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; }
}
我注册的 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);
}
我在哪里调用我的 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);
}
编辑
伙计们,我使用以下 link 中的 Ameni 主题解决了我的问题:download file using angularJS and asp.net mvc
我需要做的就是像 Ameni 主题上的 Post 方法和按路径下载功能一样创建。
只需 add/use download
属性即可。有关下载属性的更多信息,请参见 w3c download documentation。
<a href="{{midia.path}}" download="filename">Download</a>