How to give a file name to uploadFile EF C#

Viewed 21

I am uploading a file and wish to name it with one of the input fields which I wille be typing in my view. For ex: I type "Test" in my "Designation Commerciale" field. However, it gives me the NullReferenceException as it does not find any. Would appreciate your help. Thanks.

Controller:

public async Task<string> UploadFile(IFormFile file)
        {
            //bool iscopied;
           
            string resp = String.Empty;
            try
            {
                if (file.Length > 0)
                {
                    var model = new IdentificationProduitViewModel();
                    string x = model.DesignationCommerciale;
                    string filename = x + Path.GetExtension(file.FileName);
                    string path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "UploadFds"));
                    using (var filestream = new FileStream(Path.Combine(path, filename), FileMode.Create))
                    {
                        await file.CopyToAsync(filestream);
                    }
                    //iscopied = true;
                    resp = filename;
                }
                else
                {
                   // iscopied = false;
                    resp = String.Empty;
                }
            }
            catch(Exception)
            {
                throw;
            }
            return resp;
        }

HTTPPOST:

      string tryupload = await UploadFile(file_fds);
                if (!String.IsNullOrEmpty(tryupload))
                {
                    TempData["uploadok"] = "Fichier chargé avec success !";
                    model.Fds_Filepath = tryupload;
                }
0 Answers
Related