【发布时间】:2019-12-14 15:25:59
【问题描述】:
我正在尝试通过 wcf 从 angular 前端上传图像。它工作正常,我也收到一条成功消息,但保存的图像未在任何其他图像程序的图像查看器中打开。
保存接收到的文件流的代码是从 stackoverflow 上一个答案复制的,但该答案非常旧。
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";
}
}
如何以正确的格式保存文件。
Angular 文件是
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 !!');
});
}
}
此服务似乎只保存了 139kb 的文件并且流正在中断。 webconfig绑定设置如下
<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>
【问题讨论】:
-
您的 JPEG 图像是否小于 10,000 字节?这就是您调用
stream.Read(buffer, 0, 10000)时所阅读的全部内容。
标签: c# angular wcf file-upload service