Created
October 4, 2018 13:56
-
-
Save stevenknox/2a3bec2baeec02fcb9e00a4dc9022012 to your computer and use it in GitHub Desktop.
Upload file to WenAPI using import ngx-file-drop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="center" *ngIf="!processingUpload && job.currentStage == 1"> | |
<file-drop headertext="Drop scrape database here" (onFileDrop)="uploadFile($event)"> | |
</file-drop> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public uploadFile(event) { | |
this.processingUpload = true; | |
let formData = new FormData(); | |
for (let i = 0; i < event.files.length; i++) { | |
event.files[i].fileEntry.file(file => { | |
formData.append("files", file, event.files[i].relativePath); | |
if (i === event.files.length - 1) { | |
this.http | |
.post(`/api/jobs/${this.id}/uploadfiles`, formData) | |
.subscribe( | |
res => { | |
console.log("Import Complete"); | |
this.alert( | |
"success", | |
"<strong>Import Complete</strong> You can now begin preparing data for sync" | |
); | |
this.cd.detectChanges(); // changing a value via .subscribe()does not notify that it should run changedetection. | |
}, | |
err => { | |
this.alert( | |
"danger", | |
"<strong>Upload Failed</strong> Check error logs for details." | |
); | |
console.log(err); | |
this.cd.detectChanges(); | |
} | |
); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost("{id}/UploadFiles")] | |
[RequestSizeLimit(100_000_000)] | |
public async Task<IActionResult> Post(int id, List<IFormFile> files) | |
{ | |
try | |
{ | |
var formFile = files?.First(); | |
if (formFile == null || formFile?.Length == 0) | |
return this.BadRequest(); | |
var filePath = _hostingEnvironment.ContentRootPath + Constants.ScrapeFolder; | |
var fileName = $"Job-{id}{Path.GetExtension(formFile.FileName)}"; | |
Console.WriteLine("Writing file " + filePath + fileName); | |
using (var stream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create)) | |
await formFile.CopyToAsync(stream); | |
var existing = await _db.Jobs.FindAsync(id); | |
existing.DataFile = fileName; | |
await _importer.Import(ImportConfig.Setup(id, fileName, existing.NullSkus, existing.BrandMatch)); | |
existing.Status = JobStatus.DataImported; | |
existing.CurrentStage = 2; | |
await _db.SaveChangesAsync(); | |
return Ok(); | |
} | |
catch (Exception ex) | |
{ | |
return BadRequest(ex.Message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment