将具有其他属性的图像发送到api中的方法

问题描述 投票:0回答:2

我使用下面的代码发送图像以发布方法并将其另存为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()
 }

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);
    }

但是现在我需要发送有关图像(类型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()
 }

任何在数据库中发送和插入的帮助。

c# angular api web-services image-uploading
2个回答
0
投票

我认为您可以只制作一个上传文件方法,并使用文件名制作另一个用于数据插入的方法,因此它将类似于:公共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(“从此处传递的网址”,this.Url)返回this.http.post(this.Url,this.img).subscribe()}


0
投票

使用FormData将其他信息附加到api调用。

const formData = new FormData();
formData.append(file.name, file,'some-data');

您可以使用相同名称的多个值。

© www.soinside.com 2019 - 2024. All rights reserved.