Please use the code below to download the file. HTML code:
<div class="col-md-6"> <label class="control-heading">Select File</label> <input type="file" [multiple]="multiple" #fileInput (change)="selectFile($event)"> <input type="button" style="margin-top: 15px;" [disabled]="!isUploadEditable" class="data-entry-button btn-pink" (click)="uploadFile()" value="Upload" title="{{globalService.generateTooltip('upload attachment','Click to upload document.')}}" data-html="true" data-toggle="tooltip" data-placement="bottom" /> </div>
Component Code:
selectFile(event: any) { this.selectedFiles = event.target.files; } uploadFile() { this.currentFileUpload = this.selectedFiles.item(0); this.globalService.pushFileToStorage(this.currentFileUpload).subscribe(event => { if (event instanceof HttpResponse) { this.loadDocumentInfo(); this.showNotification('Upload Attachment', 'File Uploaded Successfully', 'success'); this.myInputVariable.nativeElement.value = ""; } }); this.selectedFiles = undefined; }
Global Service Code:
pushFileToStorage(file: File): Observable<HttpEvent<{}>> { const formdata: FormData = new FormData(); formdata.append('file', file); formdata.append('documentVersionId', this.documentVersionId.toString()); formdata.append('levelId', this.levelId); formdata.append('levelKey', this.levelKey); formdata.append('LoggedInUser', this.loggedInUser); const req = new HttpRequest('POST', this.urlService.CMMService + '/CMMService-service/UploadFileAsAttachment', formdata, { reportProgress: true, responseType: 'text' } ); return this.http.request(req); }
To upload a file with the name and path to the file:
call the DownloadFile function from html with the file name and path to the file as parameters.
component code:
DownloadFile(filePath: string, filename: string) { this.globalService.DownloadFile(filePath).subscribe(res => { //console.log('start download:', res); var url = window.URL.createObjectURL(res); var a = document.createElement('a'); document.body.appendChild(a); a.setAttribute('style', 'display: none'); a.href = url; res.filename = filename; a.download = res.filename; a.click(); window.URL.revokeObjectURL(url); a.remove(); // remove the element }, error => { console.log('download error:', JSON.stringify(error)); }, () => { console.log('Completed file download.') }); }
Global service code for downloading the file:
public DownloadFile(filePath: string): Observable<any> { return this.http .get(this.urlService.CMMService + '/CMMService-service/DownloadFile?filePath=' + filePath, { responseType: 'blob' }); }
on the server side, use the following code:
[HttpGet] [ODataRoute("DownloadFile")] public HttpResponseMessage DownloadFile(string filePath) { var fileData = CommonDomain.DownloadFileFromS3(filePath); var dataStream = new MemoryStream(fileData.ByteArray); HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK); httpResponseMessage.Content = new StreamContent(dataStream); httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileData.FileName; httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); httpResponseMessage.Content.Headers.Add("x-filename", fileData.FileName); return httpResponseMessage; }
Please let me know if you still encounter any problem.