TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
taha suliman
1.4k
368
6.6k
Problem uploading files via asp.net core api
Nov 29 2020 6:02 AM
Why can I upload files via the Postman application, but when I use Microsot, I cannot upload where is the problem with the following code?
Model is:
public
class
Book
{
[Key]
public
int
BookId { get; set; }
[Display(Name =
"Book name"
)]
[Required(ErrorMessage =
"Data feild is required"
)]
public
string BookName { get; set; }
[Display(Name =
"Book details"
)]
[Required(ErrorMessage =
"Data feild is required"
)]
public
string BookDetails { get; set; }
[Display(Name =
"Publish date"
)]
[DisplayFormat(DataFormatString =
"{0:yyyy-MM-dd}"
, ApplyFormatInEditMode =
true
)]
[Required(ErrorMessage =
"Data feild is required"
)]
public
DateTime BookPdate { get; set; }
[Display(Name =
"Book name"
)]
[Required(ErrorMessage =
"Data feild is required"
)]
[ForeignKey(
"Author"
)]
public
int
AuthortId { get; set; }
public
virtual Author Author { get; set; }
public
string ImagePath { get; set; }
[NotMapped]
public
IFormFile ImageFile { get; set; }
}
Client controller method:
[HttpPost]
public
IActionResult AddOrEdit(Book book)
{
if
(book.BookId == 0)
{
HttpResponseMessage resp = GlobalApiClient.webApiClient.PostAsJsonAsync(
"Books"
, book).Result;
TempData[
"SuccessMsg"
] =
"Saved Successfully ... "
;
return
RedirectToAction(
"Index"
);
}
else
{
HttpResponseMessage resp = GlobalApiClient.webApiClient.PutAsJsonAsync(
"Books/"
+ book.BookId , book).Result;
if
(resp.IsSuccessStatusCode)
{
TempData[
"SuccessMsg"
] =
"Updated Successfully ... "
;
}
else
{
TempData[
"SuccessMsg"
] =
"Updated not complete ... "
;
}
return
RedirectToAction(
"Index"
);
}
}
Api controller method:
//// PUT api/<controller>/5
[HttpPut(
"{id}"
)]
public
async Task<IActionResult> Put(
int
id,[FromBody] Book book)
{
if
(ModelState.IsValid)
{
try
{
if
(id == book.BookId)
{
string
fileName = book.ImagePath;
if
(book.ImageFile.Length > 0)
{
string
path = hosting.WebRootPath +
"\\Uploads\\"
;
if
(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
using
(FileStream fileStream = System.IO.File.Create(path + book.ImageFile.FileName))
{
book.ImageFile.CopyTo(fileStream);
fileStream.Flush();
}
}
book.ImagePath = fileName;
await bookRepo.Update(book);
return
Ok();
}
return
NotFound();
}
catch
(Exception)
{
return
BadRequest();
}
}
return
BadRequest();
}
Reply
Answers (
3
)
Saving asp.net encrypted password to sql database
I have a checkboxlist and i want to select multiple on binddata