Тёмный

Uploading Files in Blazor Web Assembly & ASP.NET Core Web API [Blazor Topics] | AK Academy 

AK Academy
Подписаться 8 тыс.
Просмотров 12 тыс.
50% 1

Опубликовано:

 

20 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 89   
@tanxinhong7963
@tanxinhong7963 Год назад
I have find so much video regarding file upload in blazor. This is the best and simplest way that works. Appreciate your effort sir.
@ahmad.mozaffar
@ahmad.mozaffar Год назад
Thank you so much for this amazing feedback
@sirajshaikh3940
@sirajshaikh3940 3 года назад
SIMPLY AWESOME CONTENT ! YOUR AMAZING.
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Thank you so much for that nice support
@aisonsystem3168
@aisonsystem3168 3 года назад
thanks for video but i have a little problem i want to store multiple file
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Hi dear That's very forget about SingleOrDefaultAsync() so this will give you a collection of file and then put them within a foreach() in the client and receive an array of files in the server instead of single file
@marcoscarvalho9956
@marcoscarvalho9956 3 года назад
Great tutorial thank you very much I will use to try to view pdf file report. Brazil Sao Paulo. Translated by Google.
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Glad it was helpful! Thanks for nice feedback
@vasylstolyarchuk6226
@vasylstolyarchuk6226 4 года назад
Thank you very much!
@bennyadiwijaya1606
@bennyadiwijaya1606 4 года назад
Hi, ahmad.. im using your method to upload file from Blazor server App to Rest API net core, but in the action controller image lenght always get 0.. Do you know why?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Thanks for reaching out Could you share more details about the code and issue via my Twitter account @AhmadMozaffar99 Thank you
@Visualcoach
@Visualcoach 3 года назад
Thanks, very usefull.
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
So happy for that thanks
@MouthBout
@MouthBout 4 года назад
I applied the RequestSizeLimit attribute to my controller post, and it works, however, there doesn't seem to be a way to return a json error response to the client when the size is exceeded. Anything I can do server-side?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi dear Basically one of the solution is to remove the size limit, and your action within the controller you can handle that manually like if(file.Length > 50000) return BadRequest("The file cannot be bigger than 5mb");
@MouthBout
@MouthBout 4 года назад
@@ahmad.mozaffar Thank you. I'm already doing that. The problem is that the entire file is uploaded first whereas RequestSizeLimit stops the request after it reaches the limit (I think).
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
I have checked that, I think this is currently not possible because the server reject the request before it processes it so you can take control of the response. The best way in this case is to make the validation for the size of the file client side then push if it's valid this quicker and safer
@102mackenzie
@102mackenzie 3 года назад
Thanks so much... do you really get up at 4:00AM? ramadan kareem
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Welcome glad you liked it Ramadan Kareem for you too Yes actually I wake up at 04:00 some times 03:30 AM but how you know that?
@raymondbell2524
@raymondbell2524 4 года назад
Very nice. But is there a limit to the file size at all? as some of the images i use don't upload and appears as a broken image
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Raymond Bell thanks a lot for your feedback Yea you can set the limit on the level of the server and on the client using the options while registering the service
@raymondbell2524
@raymondbell2524 4 года назад
@@ahmad.mozaffar HI. Whereabouts do i set that? Thanks!
@raymondbell2524
@raymondbell2524 4 года назад
@@ahmad.mozaffar Its ok i solved my problem
@RicardoJosue
@RicardoJosue 4 года назад
Hi its a great tutorial, but , When I Upload Image , the object ([FromForm] IFormFile image) image in server is null, I replicated code that this video successful , what is the problem? I wait the response, grettings from Mexico
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi dear Thanks for your feedback Did you make sure that the name of the parameter you are sending is the same of the parameter name in controller’s action?
@RicardoJosue
@RicardoJosue 4 года назад
​@@ahmad.mozaffar Oh! yeah it's correct, All work fine! Thaks for you response, and exist one way for make this operation without nuget package?
@charles-y2z6c
@charles-y2z6c 4 года назад
Please How can I get the encrypted filename back from the file controller API? The file controller is async Task Post but when I call the API from web assembly the type is HttpResponseMessage which does not return the filename, but the HTTP results. In postman it returns the filename it is getting the return Ok($"Files/{newFileName}"); I have modified it for PDF and Excel files and need to process the files later and store in a database.
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi dear Thanks for your interest and reaching out When the server sends you back a HttpResponseMessage just do like this var response = sendrequest........ var contentAsString = await response.Content.ReadAsStringAsync(); Then you have the content you can extract the filename from it Hope that helps you solve your issue
@charles-y2z6c
@charles-y2z6c 4 года назад
@@ahmad.mozaffar Got it Thanks a ton!
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Charles Happy for that
@gandhisanket16
@gandhisanket16 3 года назад
Is that possible to upload the file in the Client Project?
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Actually I didn't get the question well, could you explain more please?
@gandhisanket16
@gandhisanket16 3 года назад
@@ahmad.mozaffar Actually, I have a project where the UI part I developed in Blazor Web Assembly with (PWA), and for API I developed in DotNetCore, API is on another domain and UI is on another domain. Now I want to Upload an employee profile photo and one document like (Gov. ID proof). So, My questions are 1) How to upload photos and documents in the UI domain which is Blazor Web Assembly? 2) What is good practice to store Images and documents, either the API domain or the UI domain?
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Sorry for late reply, I totally got your point, all the files have to be on the server-side The best way to store your file is to use Azure Blob Storage, and you can have two containers within it one called ui & domain for example, it's very easy to setup and almost free.
@gandhisanket16
@gandhisanket16 3 года назад
@@ahmad.mozaffar Thank you so much.
@bennyadiwijaya1606
@bennyadiwijaya1606 4 года назад
Great tutorial, can you give tutorial how to delete image from web API?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Glad you liked it Sure I can I will mention here a link on my website for such a task in a while Thank you
@gadmonhansprajogo2215
@gadmonhansprajogo2215 4 года назад
How can I read image from API Directory?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi dear Thanks for reaching out just the ...../{thefolder and your image} for example localhost:1000:/uploads/1.jpg
@gadmonhansprajogo2215
@gadmonhansprajogo2215 4 года назад
@@ahmad.mozaffar Owh.. thankyou.. your content is great!
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Gadmon HP so happy to hear that Thanks
@hiphopheaven
@hiphopheaven 4 года назад
How can I download the files to the user computer?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi You can do that by simply retrieve the name of the folder and the name of the uploaded file concatenated with the API url for example localhost:5555/uploads/test-image-11112222.jpg Navigate to the url and you will get the files
@charles-y2z6c
@charles-y2z6c 4 года назад
For some reason OpenFileAsync is not firing onchange in the tag the open file dialog box allows to choose. But the onchage event never fires. Any thoughts ?
@charles-y2z6c
@charles-y2z6c 4 года назад
Well when I downloaded your source code on githib it works perfect. So I will try to see difference. Thanks
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hey thanks for your interest Okay great Try it and inform me with the result
@charles-y2z6c
@charles-y2z6c 4 года назад
AK Academy Thanks for the input. It appears that the project you created was while it’s still in preview. When I created it was production release 1st version. I would think something like . would be okay in a release 1 if it worked in preview, It’s old school file uploading. They do have a blazor control . but the on change event for that doesn’t fire either. My project was a professional project I am being paid for (my first of blazor ) I hate to use preview but if I can’t get it to work I may have to and upgrade it later. The risk of being an early adopter I guess. I like blazor mvvm style and want to see it work. I may try is as a blazor server _hosting
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Charles Yeah that could be the issue because the video is months ago while Blazor WASM wasn’t released yet for public but while you have the latest version of .NET and VS everything should work very fine
@charles-y2z6c
@charles-y2z6c 4 года назад
@@ahmad.mozaffar I wish you could see me slap my head and call myself a dummy. I did not put the @ in front of the onchange in the input tag I still maintain quite a few asp.net applications and it did not look like an error in my head and compiler had no issue with it. Took me 24 hours, but it was actually worth it, because I studied every byte of the code and learned a lot. I appreciate all your videos, going to do SignalR next. Thank you.
@katalepsia44
@katalepsia44 3 года назад
Hi! can you give tutorial import and display csv file asp.netcore? regards
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Yea that’s a cool idea but meanwhile you can take advantage of using this library joshclose.github.io/CsvHelper/ Very powerful and super easy to use
@bashirmanafikhi
@bashirmanafikhi 4 года назад
what happens if we uploaded files to client side project..?? are the files will be downloaded to user's cache ?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
بشير منافيخي - Bashir Manafikhi Sorry, I didn’t got your question
@bashirmanafikhi
@bashirmanafikhi 4 года назад
@@ahmad.mozaffar why don't we upload files to client side project?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Yea sure, in this demo I upload the file to the server via ASP.Net Core API
@prathameshshende4
@prathameshshende4 4 года назад
How do I see the file / preview before uploading file or save into database and file system ?
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
prathamesh shende Hi thanks for your interest Here you can find exactly what you want ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-UzpnwrQQ4uY.html
@prathameshshende4
@prathameshshende4 4 года назад
@@ahmad.mozaffar Thanks Buddy. Got What I need exactly.
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
prathamesh shende Happy for that
@aisonsystem3168
@aisonsystem3168 3 года назад
please help me for this problem
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Hi dear That's very forget about SingleOrDefaultAsync() so this will give you a collection of file and then put them within a foreach() in the client and receive an array of files in the server instead of single file
@charles-y2z6c
@charles-y2z6c 4 года назад
I have been trying to delete one of the files. I modified this so I can upload an excel file and store the file in a database for the company. The excel file is processed and stored in a database. When it’s all done I want to delete the file. I have been trying to use the system.io library, nothing seems to work. File.Delete(). File.Exist() I am 100% I am referenced the right file. .net core no longer supports Server.MapPath()
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Hi dear Thanks for reaching out Could you please provide me with more details about the issue And yes .NET Core doesn't support Server.MapPath() you can do this instea Directory.GetCurrentDirectory() + "/wwwroot/" + "YOUR FILES PATH in THE PROJECT"
@charles-y2z6c
@charles-y2z6c 4 года назад
@@ahmad.mozaffar Thank you, didn't think of wwwroot I assumed it was / as root. so I am doing Directory.GetCurrentDirectory() + "/files/" Let me try that. One issue that is interesting is File.Exists() does not throw error if the file does not exist. Which makes sense.
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Charles Happy to know that it works fine
@argeelearner3978
@argeelearner3978 3 года назад
Wrong link in video description
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Hi dear Which link exactly?, the only important link for this video is the GitHub repo and it's working fine
@argeelearner3978
@argeelearner3978 3 года назад
@@ahmad.mozaffar The link to "Getting Started with Blazor Web Assembly:"
@KamalTimalsina
@KamalTimalsina 4 года назад
can you help me to find out my problem
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
Surely dear Here to help
@KamalTimalsina
@KamalTimalsina 4 года назад
@@ahmad.mozaffar please..see code below... model public class TblstudentDocument { public int Id { get; set; } public int? StudentId { get; set; } public string DocumentType { get; set; } public int? Typeid { get; set; } public string DocumentName { get; set; } public string DocumentFile { get; set; } public int? Status { get; set; } public IFormFile UploadedFile { get; set; } } This is API [HttpPost("Save")] public async Task DocumentInsert( [FromForm]TblstudentDocument document) { var file = Request.Form.Files; if (file == null) { return BadRequest(new { code = "08", Message = "Empty Model" }); } else { using (connection) { try { connection.Open(); document.UploadedFile = file[0]; var returnvalue = await GetRepo.saveDocument(document, connection); if (returnvalue != null) { return Ok(new { code = "03", Message = "Data Saved Success", data = returnvalue }); } else { return BadRequest(new { code = "01", Message = "Data Not Saved" }); } } catch (Exception ee) { return BadRequest(new { code = "06", Message = ee.Message }); } finally { connection.Close(); } } } } my repo public async Task SaveStudentDocument(TblstudentDocument details, MemoryStream ff) { using (var content = new MultipartFormDataContent()) { content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); StringContent ss = new StringContent(JsonConvert.SerializeObject(details)); content.Add(new StreamContent(ff, (int)ff.Length), "UploadedFile",details.DocumentFile); content.Add(new StringContent(details.DocumentType), "DocumentType"); content.Add(new StringContent(details.DocumentName), "DocumentName"); content.Add(new StringContent(details.StudentId.ToString()), "StudentId"); content.Add(new StringContent(details.Typeid.ToString()), "typeid"); content.Add(ss, "data"); var response = await httpClient.PostAsync("api/Document/save", content); return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } throw new System.NotImplementedException(); } Everything ok till this repo but when memoryStream get data but when i recived data in api the length of data is 0
@KamalTimalsina
@KamalTimalsina 4 года назад
@@ahmad.mozaffar i am using blazor server app
@KamalTimalsina
@KamalTimalsina 4 года назад
And it works fine when test from postman
@ahmad.mozaffar
@ahmad.mozaffar 4 года назад
When push the request from Blazor, Do you get only the file as 0 length or also other properties come empty?
@tofronhtc
@tofronhtc 3 года назад
for this content i will glady subscribe!!! can you make content for how to do push notification(even when you are not using the app, like in android) for blazor using web api. so when im installing blazor in android, user get push notification. i will gladly paid for that content. btw im using web api with bearer token authentication.
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Thanks happy for that. And this is exactly what I’m doing, a new course is being recorded called “Advanced Notifications System with ASP.NET Core 5.0, SignalR, Blazor WebAssembly and UWP apps” It will be online at the beginning of February
@tofronhtc
@tofronhtc 3 года назад
@@ahmad.mozaffar what i meant was push notification like android notification center like when you got new message in whatsapp. Not like fb new notification when we got red button in chiming bell icon. Is it the same with your course direction?
@ahmad.mozaffar
@ahmad.mozaffar 3 года назад
Ah okay you mean push notifications a native notification from the browser to the operating system
@tofronhtc
@tofronhtc 3 года назад
@@ahmad.mozaffar yes. Like the example microsoft team put in blazing pizza app. But with api.
Далее
Brutally honest advice for new .NET Web Developers
7:19
Uploading Files with Blazor (Server & WebAssembly)
7:17