我使用以下代码将图像发送到post方法并将其保存为DB中的BLOB,并且它已成功运行:
角度代码:
public postUploadedFile(file:any){
this.formData = new FormData();
this.formData.append('file',file,file.name);
this.Url='http://localhost:38300/api/site/PostUploadFiles';
console.log("url passed from here",this.Url)
return this.http.post(this.Url , this.img).subscribe()
}
接口代码:
public IHttpActionResult PostUploadFiles()
{
int i = 0;
var uploadedFileNames = new List<string>();
string result = string.Empty;
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
while(i < httpRequest.Files.Count && result != "Failed")
{
br = new BinaryReader(httpRequest.Files[i].InputStream);
ImageData = br.ReadBytes(httpRequest.Files[i].ContentLength);
br.Close();
if (DB_Operation_Obj.Upload_Image(ImageData) > 0)
{
result = "success";
}
else
{
result = "Failed";
}
i++;
}
}
else
{
result = "can't find images";
}
return Json(result);
}
但是现在我需要发送带有图像(类型 id、名称(的更多信息,而不仅仅是图像,所以 angular 代码将如下所示:
public postUploadedFile(file:any, type_id:number,site_id:number){
this.img = new Image_List();
this.img.images = new Array<PreviewURL>();
this.img.type_id= type_id;
this.img.Reference_id = site_id;
this.img.images.push(file);
this.formData = new FormData();
this.formData.append('file',file,file.name);
this.Url='http://localhost:38300/api/site/PostUploadFiles';
console.log("url passed from here",this.Url)
return this.http.post(this.Url , this.img).subscribe()
}
任何帮助发送和插入数据库。
我认为您可以只制作一个上传文件方法,并使用文件名制作另一种数据插入方法,因此它将如下所示: public postUploadedFile(file:any({ this.formData = new FormData((; this.formData.append('file',file,file.name(; this.url='http://localhost:38300/api/site/PostUploadFiles'; this.newMethod(filename(;//在这里你上传其他数据 console.log("url 从这里传递",这个。网址(返回 this.http.post(这.url , this.img(.subscribe(( }
使用 FormData 将其他信息追加到 API 调用。
const formData = new FormData();
formData.append(file.name, file,'some-data');
可以使用具有相同名称的多个值。