Michael Wood Michael Wood - 3 years ago 188
ASP.NET (C#) Question

HTML5 Camera Image XMLHttpRequest file is empty

I am attempting to read the camera using HTML5 and XMLHttpRequest. I have tried a couple of things but no matter what I change I can not seem to actually send the Form Data, data through the post. So can someone tell me where I am making the error and not generating any image to save. Alternatively if you don't have to call out to a url but instead use a local function that would be awesome.

aspx file and the HTML structure.

<div class="row" runat="server" id="div_Upload" visible="false">
<hr />
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" class="btn btn-shadow" id="fileToUpload" accept="image/*" capture="camera" onchange="fileSelected()" />
<input type="button" name="btn_upload" value="Upload" id="btn_upload" onclick="uploadFile()" class="btn btn-shadow" />
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
<div id="progressNumber"></div>
<hr />

The javascript in the head of the file:

<script type="text/javascript">

function fileSelected() {
var file = document.getElementById('fileToUpload').files[0];
alert("fileSelected : " + file.name);
if (file) {
var fileSize = 0;
if (file.size > 1024 * 1024)
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;
document.getElementById('fileType').innerHTML = 'Type: ' + file.type;

function uploadFile() {
var fd = new FormData();
var file = document.getElementById('fileToUpload').files[0]
alert("upload File : " + file.name);
fd.append(file.name, file);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "SaveToFile.aspx");
alert("xhr Response: " + xhr.response);

function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
else {
document.getElementById('progressNumber').innerHTML = 'unable to compute';

function uploadComplete(evt) {
/* This event is raised when the server send back a response */

function uploadFailed(evt) {
alert("There was an error attempting to upload the file.");

function uploadCanceled(evt) {
alert("The upload has been canceled by the user or the browser dropped the connection.");


And here is the SaveToFile.aspx

<%@ Page Language="C#" %>

HttpFileCollection files = HttpContext.Current.Request.Files;

for (int index = 0; index < files.Count; index++)
HttpPostedFile uploadfile = files[index];
uploadfile.SaveAs(Server.MapPath(".") + "\\upload\\" + uploadfile.FileName);
HttpContext.Current.Response.Write("Upload successfully!");


By the time I am calling the SaveToFile.aspx page there are no files sent and the file count = 0;

Any help would be greatly appreciated! And a way to keep within the same page would be awesome. I saw a reference to that in another post but when I call a local function, I get a 404 error.

Answer Source

With asp.net and webforms, the form information was incomplete.

I went from


on the site master page to

<form id="form1" runat="server" enctype="multipart/form-data">

This fixed all issues and several different options to get data from client side, fixed themselves once I made the change.

With a master page I don't know of a different way to resolve this but this works with the site master.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download