开发者

File API File Upload - Read XMLHttpRequest in ASP.NET MVC

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 app开发者_高级运维reciated!


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);
})();


This is great. Improved slightly:

xhr.setRequestHeader("X-File-Name", file.name);
xhr.setRequestHeader("X-File-Size", file.size);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜