We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to get file from datagrid dialog upload

Hi!

How to get file (content) in the CRUD action on the Controller?

I send one file (and some property) from the datagrid custom dialog (like at a form):
<ejs-uploader id="photo" autoUpload="false" multiple="false" allowedExtensions="image/*" />

The controller received photo parameter is string: "C:\fakepath\[Filename].[extension]"

How to get file content?

I try 
byte[] photo = System.IO.File.ReadAllBytes(model.value.Photo);
But received exception: System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\fakepath\[filename].[extension]'.'

4 Replies

PB Peter Barasits September 5, 2019 05:08 PM UTC

I use this demo code: https://ej2.syncfusion.com/aspnetcore/Uploader/FormSupport#/material
I need the controller code, to the get uploaded file content.

[code]
[HttpPost]
public IActionResult FormData(DataModel model){
  byte[] photo = System.IO.File.ReadAllBytes(model.value.Photo);  // And this get exception
}
[/code]

Model is get correctly, but file get only file path, and path getting started c:\fakepath\
What the problem?


NP Narayanasamy Panneer Selvam Syncfusion Team September 6, 2019 12:30 AM UTC

Hi Peter, 
 
Good day to you. 
 
Yes, you can convert uploaded files to binary format. Please refer the below link: https://ej2.syncfusion.com/aspnetcore/documentation/uploader/how-to/convert-image-into-binary-format-after-uploading/ 
Please let us know if you need any further assistance on this. 
 
Regards,  
Narayanasamy P.  
10


PB Peter Barasits September 6, 2019 07:35 AM UTC

Hi Narayanasamy,

I tried this, but not working.
If set autoupload=false and send the form, will not call a save action.
If set autoupload=true, call a save action, but not send a file (UploadFiles.Count = 0).


SN Sevvandhi Nagulan Syncfusion Team September 9, 2019 02:22 PM UTC

Hi Peter, 


Query 1: While setting autoupload=false and send the form, will not call a save action 
 
As we mentioned in the below documentation,autoUpload must be disabled and save& remove url must be null when you placing the uploader inside form. This is default behaviour. 

Query2: while setting autoupload=true, call a save action, but not send a file -UploadFiles.Count = 0 
 
This issue might occur when you have placed the submit button outside of the form. So we suggest you place the submit button inside the form. Also, you can get the files in the controller using the specified name attribute in control. Please refer the below code, 
 
[index.cshtml] 

          <form id="form1" method="post" action="/Home/Save"> 
              <ejs-uploader id="fileupload" autoUpload="false" multiple="false" allowedExtensions="image/*"> </ejs-uploader> 
              <div class="submitBtn"> 
                  <button class="submit-btn e-btn" id="submit-btn" type="submit">Submit</button> 
                  <div class="desc"><span>*This button is not a submit type and the form submit handled from externally.</span></div> 
              </div> 
          </form> 

[HomeController.cs] 
public IActionResult Save(IList<IFormFile> fileupload) 
        { 
            try 
            { 
                foreach (var file in fileupload) 
                { 
                    if (fileupload != null) 
                    { 
                        var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); 
                        filename = hostingEnv.WebRootPath + $@"\{filename}"; 
                        if (!System.IO.File.Exists(filename)) 
                        { 
                            //using (FileStream fs = System.IO.File.Create(filename)) 
                            //{ 
                            //    file.CopyTo(fs); 
                            //    fs.Flush();  
                            //} 
                        } 
                        else 
                        { 
                            Response.Clear(); 
                            Response.StatusCode = 204; 
                            Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists."; 
                        } 
                    } 
                } 
            } 
            catch (Exception e) 
            { 
                Response.Clear(); 
                Response.ContentType = "application/json; charset=utf-8"; 
                Response.StatusCode = 204; 
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content"; 
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message; 
            } 
            return Content(""); 
        } 

Please find the sample from below link, 


Regards, 
Sevvandhi N 


Loader.
Live Chat Icon For mobile
Up arrow icon