Please help me with my problem.
I am creating a website which the user can upload a word document to be converted to pdf. and the user should be able to download the processed file.
As of now i have this Html code
<input type="file" (change)="upload($event.target.files)" />
Upload Percent: {{percentDone}}% <br />
<ng-container *ngIf="uploadSuccess">
<button type="button">Download</button>
</ng-container>
And this is the Angular Code
upload(files: File[]){
this.uploadAndProgress(files);
}
uploadAndProgress(files: File[]){
console.log(files)
var formData = new FormData();
Array.from(files).forEach(f => formData.append('file',f))
this.http.post('http://localhost:5000/api/DocumentToPdf/Document', formData, {reportProgress: true, observe: 'events'})
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.percentDone = Math.round(100 * event.loaded / event.total);
} else if (event instanceof HttpResponse) {
this.uploadSuccess = true;
}
});
}
And this is the Asp.net Core code
[Route("api/[controller]")]
[ApiController]
public class DocumentToPdfController : ControllerBase
{
private readonly IDocumentToPdf _documentToPdf;
public DocumentToPdfController(IDocumentToPdf documentToPdf)
{
_documentToPdf = documentToPdf;
}
[HttpPost("Document")]
public async Task<Stream> Document()
{
try
{
var file = Request.Form.Files;
return _documentToPdf.DocumentToPdf(file[0].OpenReadStream());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw;
}
}
}
I was able to process the uploaded file.
But the problem is i don't have a functionality to download the output file.
As of now the return of the Asp.net core is Stream.
How can i make adjust it to be able to download the file process?
If i'm going to write the file processed in the server side. How can i create a download link which will be able to download the output file?
Update: As per sdev95 Comment. i changed my code into
Controller:
public HttpResponseMessage Document()
{
try
{
var file = Request.Form.Files;
var fileData = _documentToPdf.DocumentToPdf(file[0].OpenReadStream());
HttpResponseMessage response = null;
response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new ByteArrayContent(fileData.ToArray())
};
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "pdf.pdf";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength = fileData.Length;
Console.WriteLine(fileData.Length);
return response;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw;
}
}
And the angular code
upload(files: File[]){
this.uploadAndProgress(files);
}
uploadAndProgress(files: File[]){
console.log(files)
var formData = new FormData();
Array.from(files).forEach(f => formData.append('file',f))
this.http.post('http://localhost:5000/api/DocumentToPdf/Document', formData, {reportProgress: true, responseType: 'blob', observe: 'events'})
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.percentDone = Math.round(100 * event.loaded / event.total);
} else if (event instanceof HttpResponse) {
this.uploadSuccess = true;
console.log('Success');
console.log('event: ' + event);
this.downloadFile(event, 'download.pdf');
}
});
}
downloadFile(data: any, filename: string) {
const blob = new Blob([data], { type: 'application/octet-stream' });
saveAs(blob, filename);
}
But my problem now is. It download a file but the file downloaded is only 1kb.
I debug the asp.net core and the code fileData.Length
is returning something like 49648
but why does it only return a 1kb file. What's wrong with my code?
Thank you