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
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?
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?
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");
@@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).
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
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
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
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?
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.
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
@@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?
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.
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
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 ?
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
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
@@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.
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
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()
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"
@@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 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
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.
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
@@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?