Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:54:56+00:00 2026-05-20T09:54:56+00:00

I am trying to implement Gmail style drag-and-drop file upload in ASP.NET MVC. I

  • 0

I am trying to implement Gmail style drag-and-drop file upload in ASP.NET MVC.

I have been following this article: http://robertnyman.com/html5/fileapi-upload/fileapi-upload.html and want to post uploaded files to an MVC controller action.

To do this, I modified the sample JavaScript script in the link to point to my controller action:

xhr.open("post", "/home/UploadFiles", true);

Here is my controller action:

[HttpPost]
public virtual string UploadFiles(object obj)
{
    var length = Request.ContentLength;
    var bytes = new byte[length];
    Request.InputStream.Read(bytes, 0, length);
    // var bytes has byte content here. what do do next?

    return "Files uploaded!";
}

I set a breakpoint, and when I upload a file, the breakpoint gets hit – which is good. But how do I extract the data from the uploaded (javascript) XMLHttpRequest object? I don’t think its in the HttpRequest – is it the parameter? If so, what type should i expect & how do I extract the byte array and extract the uploaded file info from it?

(I am using Chrome – I know it doesn’t work in IE)

Any suggestions would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:54:57+00:00Added an answer on May 20, 2026 at 9:54 am

    Figured it out. Here is the C# code:

    [HttpPost]
        public virtual string UploadFiles(object obj)
        {
            var length = Request.ContentLength;
            var bytes = new byte[length];
            Request.InputStream.Read(bytes, 0, length);
            // bytes has byte content here. what do do next?
    
            var fileName = Request.Headers["X-File-Name"];
            var fileSize = Request.Headers["X-File-Size"];
            var fileType = Request.Headers["X-File-Type"];
    
            var saveToFileLoc = string.Format("{0}\\{1}",
                                           Server.MapPath("/Files"),
                                           fileName);
    
            // save the file.
            var fileStream = new FileStream(saveToFileLoc, FileMode.Create, FileAccess.ReadWrite);
            fileStream.Write(bytes, 0, length);
            fileStream.Close();
    
            return string.Format("{0} bytes uploaded", bytes.Length);
        }
    

    And here’s the Javascript code:

    <script type="text/javascript">
    (function ()
    {
        var filesUpload = document.getElementById("files-upload");
        var dropArea = document.getElementById("drop-area");
        var fileList = document.getElementById("file-list");
    
        function uploadFile(file)
        {
            var li = document.createElement("li");
            var progressBarContainer = document.createElement("div");
            var progressBar = document.createElement("div");
    
            progressBarContainer.className = "progress-bar-container";
            progressBar.className = "progress-bar";
            progressBarContainer.appendChild(progressBar);
            li.appendChild(progressBarContainer);
    
            // Uploading - for Firefox, Google Chrome and Safari
            var xhr = new XMLHttpRequest();
    
            // Update progress bar
            xhr.upload.addEventListener("progress", function (evt)
            {
                if (evt.lengthComputable)
                {
                    progressBar.style.width = (evt.loaded / evt.total) * 100 + "%";
                }
            }, false);
    
            // File uploaded
            xhr.addEventListener("load", function ()
            {
                progressBarContainer.className += " uploaded";
                progressBar.innerHTML = "Uploaded!";
            }, false);
    
            xhr.open("post", "/home/UploadFile", true);
    
            // Set appropriate headers
            xhr.setRequestHeader("Content-Type", "multipart/form-data");
            xhr.setRequestHeader("X-File-Name", file.fileName);
            xhr.setRequestHeader("X-File-Size", file.fileSize);
            xhr.setRequestHeader("X-File-Type", file.type);
    
            // Send the file
            xhr.send(file);
    
            // Present file info and append it to the list of files
            var div = document.createElement("div");
            li.appendChild(div);
            var fileInfo = "<div><strong>Name:</strong> " + file.name + "</div>";
            fileInfo += "<div><strong>Size:</strong> " + parseInt(file.size / 1024, 10) + " kb</div>";
            fileInfo += "<div><strong>Type:</strong> " + file.type + "</div>";
            div.innerHTML = fileInfo;
    
            // insert at beginning of list.
            fileList.insertBefore(li, fileList.firstChild);
    
            // or insert at end of list.
            //fileList.appendChild(li);
        }
    
        function traverseFiles(files)
        {
            if (typeof files !== "undefined")
            {
                for (var i = 0, l = files.length; i < l; i++)
                {
                    uploadFile(files[i]);
                }
            }
            else
            {
                fileList.innerHTML = "No support for the File API in this web browser";
            }
        }
    
        filesUpload.addEventListener("change", function ()
        {
            traverseFiles(this.files);
        }, false);
    
        dropArea.addEventListener("dragleave", function (evt)
        {
            var target = evt.target;
    
            if (target && target === dropArea)
            {
                this.className = "";
            }
            evt.preventDefault();
            evt.stopPropagation();
        }, false);
    
        dropArea.addEventListener("dragenter", function (evt)
        {
            this.className = "over";
            evt.preventDefault();
            evt.stopPropagation();
        }, false);
    
        dropArea.addEventListener("dragover", function (evt)
        {
            evt.preventDefault();
            evt.stopPropagation();
        }, false);
    
        dropArea.addEventListener("drop", function (evt)
        {
            //document.getElementById("file-list").innerHTML = "";
    
            traverseFiles(evt.dataTransfer.files);
            this.className = "";
            evt.preventDefault();
            evt.stopPropagation();
        }, false);
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I'm trying to implement a file upoload system similar to gmail's. I've already done
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
Trying to implement the following behavior on an iPad. I have a map-centric application
trying to implement a dialog-box style behaviour using a separate div section with all
I'm trying to implement adaptive payments but keep getting this weird error. Here's the
I'm trying to create an upload file, and email as an attachment form where
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
Trying to implement an MVC pattern, keep coming back to a NullPointerException . I
Trying to implement what I thought was a simple concept. I have a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.