формирование списков работает
This commit is contained in:
parent
a166e976de
commit
5333817ea8
@ -115,7 +115,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts
|
|||||||
});
|
});
|
||||||
|
|
||||||
byte[] file = File.ReadAllBytes(model.FileName);
|
byte[] file = File.ReadAllBytes(model.FileName);
|
||||||
//File.Delete(model.FileName);
|
File.Delete(model.FileName);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,21 +47,23 @@ namespace HardwareShopRestApi.Controllers
|
|||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public void BuildPurchaseReport(PurchaseBindingModel model, string format, string filename)
|
public byte[] BuildPurchaseReport(PurchaseBindingModel model, string format)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
byte[] file;
|
||||||
switch (format)
|
switch (format)
|
||||||
{
|
{
|
||||||
case "doc":
|
case "docx":
|
||||||
_reportWorkerLogic.SavePurchasesToWordFile(new ReportBindingModel { FileName = filename }, model.Purchases);
|
file = _reportWorkerLogic.SavePurchasesToWordFile(new ReportBindingModel { FileName = "temp.docx" }, model.Purchases);
|
||||||
break;
|
break;
|
||||||
case "excel":
|
case "xlsx":
|
||||||
_reportWorkerLogic.SavePurchasesToExcelFile(new ReportBindingModel { FileName = filename }, model.Purchases);
|
file = _reportWorkerLogic.SavePurchasesToExcelFile(new ReportBindingModel { FileName = "temp.xlsx" }, model.Purchases);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FormatException("Неправильный формат файла");
|
throw new FormatException("Неправильный формат файла");
|
||||||
}
|
}
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -46,5 +46,23 @@ namespace HardwareShopWorkerApp
|
|||||||
throw new Exception(result);
|
throw new Exception(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static R? PostRequestWithResult<T, R>(string requestUrl, T model)
|
||||||
|
{
|
||||||
|
var json = JsonConvert.SerializeObject(model);
|
||||||
|
var data = new StringContent(json, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
var response = _client.PostAsync(requestUrl, data);
|
||||||
|
|
||||||
|
var result = response.Result.Content.ReadAsStringAsync().Result;
|
||||||
|
if (response.Result.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return JsonConvert.DeserializeObject<R>(result);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -332,7 +332,7 @@ namespace HardwareShopWorkerApp.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public void ListComponents([FromBody] PurchaseBindingModel goodModel, [FromQuery] string format, [FromQuery] string filename)
|
public int[]? ListComponents([FromBody] PurchaseBindingModel purchaseModel, [FromQuery] string format)
|
||||||
{
|
{
|
||||||
if (APIClient.User == null)
|
if (APIClient.User == null)
|
||||||
{
|
{
|
||||||
@ -342,11 +342,11 @@ namespace HardwareShopWorkerApp.Controllers
|
|||||||
{
|
{
|
||||||
throw new FormatException($"Неправильный формат файла: {format}");
|
throw new FormatException($"Неправильный формат файла: {format}");
|
||||||
}
|
}
|
||||||
if (string.IsNullOrEmpty(filename))
|
byte[]? file = APIClient.PostRequestWithResult<PurchaseBindingModel, byte[]>($"api/report/buildpurchasereport?format={format}", purchaseModel);
|
||||||
{
|
var array = file!.Select(b => (int)b).ToArray();
|
||||||
throw new FormatException($"Неправильное название файла: {filename}");
|
return array;
|
||||||
}
|
|
||||||
APIClient.PostRequest($"api/report/buildpurchasereport?format={format}&filename=${filename}", goodModel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
@ -23,10 +23,6 @@
|
|||||||
<tbody id="result">
|
<tbody id="result">
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<div class="col-sm-3">
|
|
||||||
<label class="form-label">Название файла</label>
|
|
||||||
<input type="text" class="form-control" name="filename" id="filename">
|
|
||||||
</div>
|
|
||||||
<div class="col d-flex justify-content-evenly align-items-baseline">
|
<div class="col d-flex justify-content-evenly align-items-baseline">
|
||||||
<button type="button" class="btn btn-primary btn-lg m-2" id="savedoc">Сохранить в doc-формате</button>
|
<button type="button" class="btn btn-primary btn-lg m-2" id="savedoc">Сохранить в doc-формате</button>
|
||||||
<button type="button" class="btn btn-primary btn-lg m-2" id="saveexcel">Сохранить в xls-формате</button>
|
<button type="button" class="btn btn-primary btn-lg m-2" id="saveexcel">Сохранить в xls-формате</button>
|
||||||
@ -59,7 +55,6 @@
|
|||||||
const resultTable = document.getElementById("result");
|
const resultTable = document.getElementById("result");
|
||||||
const saveDocBtn = document.getElementById("savedoc");
|
const saveDocBtn = document.getElementById("savedoc");
|
||||||
const saveExcelBtn = document.getElementById("saveexcel");
|
const saveExcelBtn = document.getElementById("saveexcel");
|
||||||
const filename = document.getElementById("filename");
|
|
||||||
|
|
||||||
submitPurchaseBtn.addEventListener("click", () => {
|
submitPurchaseBtn.addEventListener("click", () => {
|
||||||
console.log('try to add purchase')
|
console.log('try to add purchase')
|
||||||
@ -88,32 +83,53 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
saveDocBtn.addEventListener("click", async () => {
|
saveDocBtn.addEventListener("click", async () => {
|
||||||
send('doc')
|
send('docx')
|
||||||
})
|
})
|
||||||
|
|
||||||
saveExcelBtn.addEventListener("click", async () => {
|
saveExcelBtn.addEventListener("click", async () => {
|
||||||
send('excel')
|
send('xlsx')
|
||||||
})
|
})
|
||||||
|
|
||||||
function send(format) {
|
function send(format) {
|
||||||
console.log(`try to save in ${format} format`)
|
console.log(`try to save in ${format} format`)
|
||||||
if (list.length == 0 || !filename.value || filename.value == '') {
|
if (list.length == 0) {
|
||||||
alert('operation failed. purchases or filename are empty')
|
alert('operation failed. purchases or filename are empty')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: `/Home/ListComponents?format=${format}&filename=${filename.value}`,
|
url: `/Home/ListComponents?format=${format}`,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
data: JSON.stringify({ "Purchases" : list })
|
data: JSON.stringify({ "Purchases" : list })
|
||||||
}).done(() => {
|
}).done((file) => {
|
||||||
//let byteArray = new Uint8Array(file);
|
let byteArray = new Uint8Array(file);
|
||||||
|
saveFile(byteArray, format);
|
||||||
})
|
})
|
||||||
.fail(function(xhr, textStatus, errorThrown) {
|
.fail(function(xhr, textStatus, errorThrown) {
|
||||||
alert(xhr.responseText);
|
alert(xhr.responseText);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function saveFile(bytes, format) {
|
||||||
|
if (window.showSaveFilePicker) {
|
||||||
|
const opts = {
|
||||||
|
suggestedName: `listcomponents.${format}`,
|
||||||
|
types: [{
|
||||||
|
description: `${format} file`,
|
||||||
|
accept:
|
||||||
|
{
|
||||||
|
[`text/${format}`]: [`.${format}`]
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
const handle = await showSaveFilePicker(opts);
|
||||||
|
const writable = await handle.createWritable();
|
||||||
|
await writable.write(bytes);
|
||||||
|
writable.close();
|
||||||
|
alert('done')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function reloadTable() {
|
function reloadTable() {
|
||||||
resultTable.innerHTML = ''
|
resultTable.innerHTML = ''
|
||||||
let count = 0;
|
let count = 0;
|
||||||
@ -132,5 +148,6 @@
|
|||||||
list = list.filter(value => value.id != resultTable.rows[id].cells[0].innerText)
|
list = list.filter(value => value.id != resultTable.rows[id].cells[0].innerText)
|
||||||
reloadTable()
|
reloadTable()
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user