Click here to Skip to main content
15,916,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How do i read files form html control to C# code and how to save in server?

I have one html input control in my html page. When i click on upload i want to save that file in server folder using .net code. but how can i read and save that file.
Below is my code,
Angular js side code,
JavaScript
function RRUplaod() {
    var File;
    File = $('#FileUpload')[0].files[0];
    if (File != "") {
    var fileName = File.name;
    fileName = fileName + '^' + id
    var xhr = new XMLHttpRequest();
    var url = //service url
    xhr.open('POST', url, true);
    xhr.setRequestHeader("FN", fileName);
    inviteFileName = fileName;
    xhr.onreadystatechange = function () {
    };

    xhr.send(File); 
}

C# code
C#
public UploadedFile UplaodFile(Stream Uploading)
{
      IncomingWebRequestContext woc = WebOperationContext.Current.IncomingRequest;
       WebHeaderCollection headers = woc.Headers;
       string fileName = "";
       string id= "";

       fileName = headers["FN"].Split('^')[0];
       id= headers["FN"].Split('^')[1];

       UploadedFile upload = new UploadedFile
       {
           FilePath = Path.Combine(HttpContext.Current.Server.MapPath(".") + "\\Document\\", fileName)
       };


       FileStream fs = new FileStream(upload.FilePath, FileMode.Open, FileAccess.Read);
       byte[] ImageData = new byte[fs.Length];
       fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
       fs.Close();
}

but i dont know how to save that file using c# code...
Please help
Posted
v2

1 solution

Refer - Save byte array to file[^].
Quote:
You can use:

C#
File.WriteAllBytes("Foo.txt", arrBytes); // Requires System.IO

If you have an enumerable and not an array, you can use:

C#
File.WriteAllBytes("Foo.txt", arrBytes.ToArray()); // Requires System.Linq
 
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