У меня проблема с загрузкой изображения, которое не хранится в локальной папке. Пожалуйста, дайте ваши предложения. Когда я загружаю (один) файл в Angular. Не могу сохранить загруженный файл в локальное хранилище, используя web api c #. ttttttttttttttttttttttttttttttttttttttttttttt
html файл
book.component.html
<form [formGroup]="bookform" enctype="multipart/form-data" (ngSubmit)="onSubmit()">
<div class="form-group">
<label class="col-sm-5 control-label" for="textinput">Playbook Image</label>
<div class="col-sm-10">
<input type="file" id="btnUpload" value="Upload" name="book_img" (change)="onSelectedfile($event)">
</div>
</div>
<div class="urlForm">
<button class="btn btn-success mr-3" type="submit">Submit</button>
<button class="btn btn-primary" type="reset" (click)="onReset()">Clear</button>
</div>
</form>
файлы сценариев типа book.component.ts
import { Component, OnInit } from '@angular/core';
import { book } from '../shared/book';
import { Router, ActivatedRoute } from '@angular/router';
import { Http, RequestOptions, Headers, Response } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NgForm, FormsModule, FormGroup, FormControl, FormArray, Validators, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-book',
templateUrl: './book.component.html',
styleUrls: ['./book.component.css']
})
export class bookComponent implements OnInit {
book: any = [];
Country: any = [];
bookform: FormGroup;
errorMessage: string;
constructor( private router: Router, private route: ActivatedRoute, private httpService: HttpClient, private http: Http) {
}
onSelectedfile(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0];
this.myFiles.push(file.name);
}
}
onSubmit() {
const formData = new FormData();
var title = this.bookform.controls['book_title'].value
var id = this.bookform.controls['id'].value
var book_file_name = this.Pbookform.controls['book_file_name'].value
formData.append("book_img", this.myFiles)
formData.append("book_title", title)
formData.append("book_file_name", book_file_name)
formData.append("id", id)
this.httpService.post('http://localhost:20589/Api/book/Addbook', formData).subscribe((data) => {
});
}
}
код на стороне сервера (web api) bookController.cs
[HttpPost]
public HttpResponseMessage Addbook()
{
try
{
HttpResponseMessage response = new HttpResponseMessage();
//var httpRequest = HttpContext.Current.Request;
var request = HttpContext.Current.Request;
var title = request["book_title"].ToString();
var name = request["book_file_name"].ToString();
var id = request["id"].ToString();
var image = request["book_img"].ToString();
if (request.Files.Count > 0)**(files not there.showing empty)**
{
foreach (string file in request.Files)
{
var postedFile = request.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName);
postedFile.SaveAs(filePath);
}
}
return response;
}
catch (Exception ex)
{
throw ex;
}
}