C# 将带有其他属性的图像发送到api中的post方法
我使用下面的代码将图像发送到post方法,并将其保存为数据库中的BLOB,并且它正在成功工作: 角度代码:C# 将带有其他属性的图像发送到api中的post方法,c#,angular,api,web-services,image-uploading,C#,Angular,Api,Web Services,Image Uploading,我使用下面的代码将图像发送到post方法,并将其保存为数据库中的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(
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()
}
API代码:
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);
}
public IHttpActionResult postPloadFiles()
{
int i=0;
var uploadedFileNames=新列表();
字符串结果=string.Empty;
HttpResponseMessage response=新的HttpResponseMessage();
var httpRequest=HttpContext.Current.Request;
如果(httpRequest.Files.Count>0)
{
而(i0)
{
结果=“成功”;
}
其他的
{
result=“失败”;
}
i++;
}
}
其他的
{
结果=“找不到图像”;
}
返回Json(结果);
}
但现在我需要发送更多的图像信息(类型id、名称),而不仅仅是图像,因此角度代码如下:
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 positionploadefile(文件:任意,类型\u id:number,站点\u id:number){
this.img=新图像列表();
this.img.images=新数组();
this.img.type\u id=type\u id;
this.img.Reference\u id=站点\u id;
this.img.images.push(文件);
this.formData=new formData();
this.formData.append('file',file,file.name);
这个.Url=http://localhost:38300/api/site/PostUploadFiles';
log(“从此处传递的url”,this.url)
返回this.http.post(this.Url,this.img).subscribe()
}
在DB中发送和插入的任何帮助。我认为您可以只制作一个上载文件方法,然后制作另一个使用文件名插入数据的方法,因此它将类似于: public postploadedfile(file:any){this.formData=new formData();this.formData.append('file',file,file.name);this.Url=''; This.newMethod(filename);//然后在这里上载其他数据
console.log(“从此处传递的url”,this.url)返回this.http.post(this.url,this.img).subscribe()}使用FormData将附加信息附加到api调用
const formData = new FormData();
formData.append(file.name, file,'some-data');
可以使用同名的多个值