Angular下載的作法:

一. 以下的作法是以XMLHttpRequest(),並以Get方式來下載.

let urlToSend= this.siteAdminService.GetBaseUrl()+ ‘Export_EditModel/’+params.em_name+’/’+params.em_id+’/’+params.language_id;
var req = new XMLHttpRequest();
req.open(“GET”, urlToSend, true);
req.responseType = “blob”;
req.onload = function (event) {
var blob = req.response;
var fileName = params.em_name+’.xlsx’; //req.getResponseHeader(“fileName”) //if you have the fileName header available
var link=document.createElement(‘a’);
link.href=window.URL.createObjectURL(blob);
link.download=fileName;
link.click();
};

req.send();

二. 另一方式,以jQuery的fileDownload來進行, $.fileDownload

const url = this.url + `DownloadFolioItems`;
$.fileDownload( url, {
httpMethod: “POST”,
data: { data: JSON.stringify(parms)},//
// tslint:disable-next-line:no-shadowed-variable
successCallback: function (url) {
if(successFunc!=null ) {
successFunc();
}
},
failCallback: function ( html, url ) {
var json = JSON.parse( html);
if(failFunc!=null){
failFunc(json);
}
}

}).done( function(e) {
console.log(e);

});

//.net 內寫法如下:
[HttpPost]
[Route(“DownloadFolioItems”)]
public ActionResult DownloadFolioItems()
{
try
{
string json = Request.Form[“data”];
JToken _parm = JsonConvert.DeserializeObject(json);
string showFileName = _parm[“showFileName”].ToString();
List item_ids = new List();
foreach (JValue item_id in _parm[“item_ids”])
{
item_ids.Add(item_id.ToString());
}
byte[] data = _managerFolio_ItemRepository.DownloadFolioItems(item_ids.ToArray(), showFileName);
return File(data, “application/octet-stream”, showFileName + “.zip”);
}
catch (Exception ex)
{
return this.Error(ex.ToString());
}
}

//Repository.cs內則:

public byte[] DownloadFolioItems(string[] item_ids, string showFileName)
{
if (item_ids == null || item_ids.Length == 0)
return null;
string item_id = “‘” + string.Join(“‘,'”, item_ids) + “‘”;
DataTable dataTable = new DataTable();
using (SqlConnection conn = new SqlConnection(cnnString))
{
conn.Open();
SqlCommand dbCommand = conn.CreateCommand();
dbCommand.CommandText = @”select b.item_id,c.remote_path+’\’+ b.item_path+’\’+b.real_filename folio_item_path,b.real_filename from folio a
inner join folio_item b on a.folio_id=b.folio_id
left join managed_location c on c.location_id=a.folio_workspace and c.purpose_type=’Folio_Workspace’ and c.status=’E’
where b.item_id in (” + item_id + “)”;
SqlDataReader dbDataReader = dbCommand.ExecuteReader();
dataTable.Load(dbDataReader);
}
if (dataTable.Rows.Count == 0)
return null;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
string filename_zip = showFileName + “.zip”;
Dictionary dictionarys = new Dictionary();
ArrayList real_filenames = new ArrayList();
foreach (DataRow dataRow in dataTable.Rows)
{
item_id = dataRow[“item_id”].ToString();
string folio_item_path = dataRow[“folio_item_path”].ToString();
string real_filename = dataRow[“real_filename”].ToString();
if (real_filenames.Contains(real_filename))
real_filename += “_” + item_id;
real_filenames.Add(real_filename);
byte[] bytes = File.ReadAllBytes(folio_item_path);
if (bytes != null)
dictionarys.Add(real_filename, new System.IO.MemoryStream(bytes));
}
if (dictionarys.Count == 0)
return null;
string path = AppDomain.CurrentDomain.BaseDirectory + (Environment.Is64BitProcess ? “x64” : “x86”) + Path.DirectorySeparatorChar + “7z.dll”;
SevenZip.SevenZipBase.SetLibraryPath(path);
SevenZipCompressor tmp = new SevenZipCompressor();
// tmp.ArchiveFormat = OutArchiveFormat.Zip;
tmp.CompressStreamDictionary(dictionarys, ms);
byte[] bs = ms.ToArray();
ms.Close();
ms.Dispose();
return bs;
}

本篇發表於 Angular。將永久鏈結加入書籤。

發佈留言