Click here to Skip to main content
15,867,289 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work on c# 7 I face issue I can't get data from file Request.form.file[0]  directly 

it must copying to server to get data from it 
so can i get data from variable DisplayFileName 

instead of get data from input path after copying to server






What I have tried:

C#
//step1 get file from angular
         var DisplayFileName = Request.Form.Files[0];
         //step2 modify file name to be unique to avoid make error if exist before 
         string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + DateTime.Now.Ticks.ToString() + ".xlsx";
         // step3 make unique input path with input data  but not create yet
         string inputpath = Path.Combine(myValue1, DateTime.Now.Month.ToString(), fileName);
         // step4 make unique output path will export final result to it  but not create yet
         string exportPath = Path.Combine(myValue2, DateTime.Now.Month.ToString(), fileName);
         CExcel ex = new CExcel();
         //step5 check input file have data
         if (fileName.Length > 0)
         {
           //step6 create file with input data i uploaded on server path on Input folder
             using (var stream = new FileStream(inputpath, FileMode.Create))
             {
                 Request.Form.Files[0].CopyTo(stream);
                  
             }
Posted
Updated 13-Sep-21 1:22am
Comments
Member 15329613 10-Sep-21 14:41pm    
It has to be copied to server for c# to get it. I do not understand your question.
ahmed_sa 10-Sep-21 16:15pm    
thanks for reply
can i get data from Request.Form.Files[0];
instead of copy to file then get data from copied file

You can read the posted contents of the file as a Stream. In ASP.NET MVC5, use the InputStream property[^]; in ASP.NET Core / .NET 5, use the OpenReadStream method[^].

Obviously whatever you're using to load data from that file will need to support reading from a stream. As an example, if you're using OLEDB to read an uploaded Excel file, that requires a physical file on your server's disk. But you may be able to use The OpenXML SDK[^] to read the file directly from the stream instead.
 
Share this answer
 
because c# run on server, if you want to get on client you should to use client side cript like java script
Please learn more about client server model
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900