Add local file to HttpPostedFileBase

poojasureshkumar
poojasureshkumar used Ask the Experts™
on
Hi All,
Advance thanks.
My existing page has upload button and can do upload feature using post method and  finally add the files from HttpRequest to  HttpPostedFileBase. This works fine.

My issue is
Have to do upload but not through drag and drops or selecting from the folder. Instead, i need to attach the local file into HttpPostedFileBase.

HttpPostedFileBase fileBase = load file using  File.Open(path, FileMode.Open);

I know it is not possible because it's  abstract class. My issue is i have to use HttpPostedFileBase but i need to load a local file into it.

please help.

Kind regards,
Pooja
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Pooja,

When you say local file is this a file on the server or on the client side?

Regards,
Chinmay.

Author

Commented:
server side
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Alright. And I understand this is an ASP.Net code so what exactly you want to do with this local file by getting it inside HttpPostedFileBase . Please don't mind but I want to understand if we can come up with something better by understanding your business requirements.
Hi Chinmay,
I got the solution. Thanks!
var binary = {here am getting binary data for the server file..}
using (var client = new HttpClient())
     {
         using (var content =
             new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
         {
             content.Add(new StreamContent(new MemoryStream(binary)), "PATH", "FileName");

              using (
                 var message =
                     await client.PostAsync("web api url", content))
              {
                  var input = await message.Content.ReadAsStringAsync();

                return null;
              }
          }
     }


Kind regards,
Pooja

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial