I am trying to upload an image through wcf from angular frontend. It is working fine and I am receiving a success message too but the image saved is not opening in image viewer of any other image program.
the code to save the received file stream is copied from stackoverflow previous answer but that answer was very old.
public string PostImage(Stream stream)
{
using (var f = new FileStream(@"C:\Temp\Sample.jpg", FileMode.OpenOrCreate))
{
stream.CopyTo(f);
}
stream.Close();
return "Recieved the image on server";
}
}
How can I save the file in correct format.
Angular files are
app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
fileData: File = null;
constructor(private http: HttpClient) { }
fileProgress(fileInput: any) {
this.fileData = fileInput.target.files[0] as File;
}
onSubmit() {
console.log('Test');
const formData = new FormData();
formData.append('file', this.fileData);
this.http.post('http://localhost:50604/Service1.svc/PostImage', formData, {responseType: 'text'})
.subscribe(res => {
console.log(res);
alert('SUCCESS !!');
});
}
}
It appears that this service is saving only 139kb file and stream is breaking. The webconfig binding settings are as follows
<webHttpBinding>
<binding name="largeMessage" maxReceivedMessageSize="1000000000000" transferMode="Streamed" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="1000000000" maxBytesPerRead="2147483647" />
<security mode="None"/>
</binding>
</webHttpBinding>