簡體   English   中英

Angular 415 不支持的媒體類型

[英]Angular 415 Unsupported Media Type

我正在嘗試將圖像上傳到文件夾並同時將字段插入數據庫,但是當我單擊保存按鈕時出現此錯誤:415 不支持的媒體類型。 有誰知道原因?

我在服務器端也做錯了嗎?

我將介紹在 asp net core 和 angular 中使用的代碼。 這是我第一次使用這項技術,可能我做錯了什么:(

Class

 public Rp CreateProduct(SqlConnection conn, Product products, IFormFile file)
        {
            Rp devolve = new Rp();
            devolve = new Rp(1, "", "", "sucess");
            SqlCommand cmd = conn.CreateCommand();
            SqlTransaction transaction;     
            conn.Open();
            transaction = conn.BeginTransaction();

            try
            {               
                cmd.CommandText = "INSERT INTO[Products](Reference, Name, Description)" +
                            " Values (@Reference, @Name, @Description)";
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@Reference", SqlDbType.VarChar).Value = Reference;
                        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
                        cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description;
                        cmd.Transaction = transaction;
                        cmd.ExecuteNonQuery();
                        transaction.Commit();
            }

            catch (Exception ex)
            {
                transaction.Rollback();
                devolve = new Rp(3, "", ex.Message, "");
            }
            finally
            {
                conn.Close();
            }
            return devolve;
        }

controller

[HttpPost]
        [Authorize]
        [Route("PostProduct")]
        public ActionResult<Rp> PostProduct([FromBody] Product p, IFormFile file, [FromQuery] string s)
        {
            try
            {

                file = Request.Form.Files[0];
                var folderName = Path.Combine("Resources", "Images");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                if (file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition)
                                    .FileName.Trim('"');
                    var fullPath = Path.Combine(pathToSave, fileName);
                    var dbPath = Path.Combine(folderName, fileName);

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }

                    return Ok(new { dbPath });
                }

                Global.Global glo = new Global.Global();
                Product prod = new Product();
                SqlConnection connection = glo.CriaConnection(configuration.GetConnectionString("BD"), s;
                Rp r = prod.CreateProduct(connection, p, file);
                return r;
            }
            catch (Exception)
            {
                return null;
            }
        }

Angular

     this.uploadService.postProduct(params).then(function (r) {
        if (r.codigo == 1) {
          notify(r.info, "success", 30);
        } else {
          notify(r.des, "error", 30);
        }
      }).catch();
    });


 postProduct(data): Promise<any> {
    let self = this;
    let urlAux = self.url + "/Products/PostProduct";

    return this.http
      .post(urlAux, data)
      .toPromise()
      .then(this.extractDataStream)
      .catch(this.handleErroPromise);
  }


constructor(private http: HttpClient) {
    this.url = localStorage.getItem("UrlS");
    this.url = this.url.replace(/"/g, "");
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      })
    }
  }
  private handleErroPromise(error: Response | any) {
    console.error(error.message || error);

    return Promise.reject(error.message || error);
  }

  private extractDataStream(res) {
    return res;
  }

嘗試刪除 typescript 文件中'Accept': 'application/json'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM