Modify the response for warranty upload. #538
This commit is contained in:
parent
9f44148e1a
commit
ba3b6c7d38
2 changed files with 59 additions and 17 deletions
|
|
@ -385,10 +385,10 @@ class WarrantyController extends Controller
|
|||
// process the csv file
|
||||
$inv_entries = $this->processWarrantyFile($file, $em, $wh);
|
||||
|
||||
$resp = new StreamedResponse();
|
||||
|
||||
if (count($inv_entries) > 0)
|
||||
{
|
||||
$resp = new StreamedResponse();
|
||||
|
||||
$resp->setCallback(function() use($inv_entries) {
|
||||
// csv output
|
||||
$csv_handle = fopen('php://output', 'w+');
|
||||
|
|
@ -421,25 +421,24 @@ class WarrantyController extends Controller
|
|||
fclose($csv_handle);
|
||||
});
|
||||
|
||||
$filename = 'invalid_warranties' . '.csv';
|
||||
|
||||
$resp->headers->set('Content-Type', 'text/csv; charset=utf-8');
|
||||
$resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
||||
$resp->setStatusCode(200);
|
||||
|
||||
return $resp;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
$resp->setCallback(function() {
|
||||
// csv output
|
||||
$csv_handle = fopen('php://output', 'w+');
|
||||
fputcsv($csv_handle, ['No Invalid Warranties']);
|
||||
$resp = new Response();
|
||||
|
||||
fclose($csv_handle);
|
||||
});
|
||||
$resp->setStatusCode(200);
|
||||
$resp->headers->set('Content-Type', 'text/html');
|
||||
|
||||
return $resp;
|
||||
}
|
||||
|
||||
$filename = 'invalid_warranties' . '.csv';
|
||||
|
||||
$resp->headers->set('Content-Type', 'text/csv; charset=utf-8');
|
||||
$resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
||||
$resp->setStatusCode(200);
|
||||
|
||||
return $resp;
|
||||
}
|
||||
|
||||
protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em,
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<form id="upload_form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('warranty_upload_submit') }}" enctype="multipart/form-data">
|
||||
<form id="upload-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('warranty_upload_submit') }}" enctype="multipart/form-data">
|
||||
<div class="m-portlet__body">
|
||||
<div class="form-group m-form__group row">
|
||||
<input type="file" id="csv_file" name="csv_file" >
|
||||
|
|
@ -50,3 +50,46 @@
|
|||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script>
|
||||
$(function() {
|
||||
$("#upload-form").submit(function(e) {
|
||||
var form = $(this);
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: form.prop('action'),
|
||||
data: new FormData(this),
|
||||
processData: false,
|
||||
contentType: false,
|
||||
cache: false,
|
||||
}).done(function(response, status, xhr) {
|
||||
if (xhr.getResponseHeader('Content-Type') == "text/html; charset=UTF-8") {
|
||||
swal({
|
||||
title: 'Done!',
|
||||
text: 'All warranties uploaded!',
|
||||
type: 'success',
|
||||
});
|
||||
} else {
|
||||
console.log('csv');
|
||||
var blob = new Blob([response], {type: "text/csv" });
|
||||
var link = document.createElement('a');
|
||||
link.href = window.URL.createObjectURL(blob);
|
||||
link.download = "invalid_warranties.csv";
|
||||
|
||||
document.body.appendChild(link);
|
||||
|
||||
link.click();
|
||||
|
||||
document.body.removeChild(link);
|
||||
|
||||
}
|
||||
}).fail(function(response) {
|
||||
});
|
||||
})
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in a new issue