I try to create a drag and drop feature for avatar images. I want the files that someone drags into the box; get uploaded to a directory: '/images/profile/$username'.
This is my code:
<div class='fileDrop'>
<span id='fileDropTarget'>Drag your files here</span>
</div>
<script>
function fileSetUploadPercent(percent) {
var uploadString = "Uploaded " + percent + " %";
$('#fileDropTarget').text(uploadString);
}
function fileUploadStarted(index, file, files_count) {
fileSetUploadPercent(0); //set the upload status to be 0
}
function fileUploadUpdate(index, file, currentProgress) {
var string = "index = " + index + " Uploading file " + file.fileName + " size is " + file.fileSize + " Progress = " + currentProgress;
$('#status').text(string);
fileSetUploadPercent(currentProgress);
}
function fileUploadFinished(index, file, json, timeDiff) {
fileSetUploadPercent(100);
$('#fileDropTarget').css('border', '2px dotted #000000').text("Upload Voltooid");
}
function fileDragLeave(event) {
$('#fileDropTarget').css('border', '2px dotted #000000').text("Sleep uw foto hierin");
}
function fileDragOver(event) {
$('#fileDropTarget').css('border', '2px dashed #000000').text("Sleep uw foto hierin");
}
$(".fileDrop").filedrop({
fallback_id: 'fallbackFileDrop',
url: '/controls/upload.ascx',
allowedfiletypes: ['image/jpeg', 'image/png', 'image/gif'], // filetypes allowed by Content-Type. Empty array means no restrictions
allowedfileextensions: ['.jpg', '.jpeg', '.png', '.gif'], // file extensions allowed. Empty array means no restrictions
// refresh: 1000,
paramname: 'fileUpload', // POST parameter name used on serverside to reference file, can also be a function taking the filename and returning the paramname
maxfiles: 1, // Ignored if queuefiles is set > 0
maxfilesize: 10, // MB file size limit
// queuefiles: 0, // Max files before queueing (for large volume uploads)
// queuewait: 200, // Queue wait time if full
// data: {},
// headers: {},
// drop: empty,
// dragEnter: empty,
dragOver: fileDragOver,
dragLeave: fileDragLeave,
// docEnter: empty,
// docOver: fileDocOver,
// docLeave: fileDocLeave,
// beforeEach: empty,
// afterAll: empty,
// rename: empty,
error: function (err, file) {
switch (err) {
case 'BrowserNotSupported':
$('#fileDropTarget').css('border', '2px dashed #FF0000').text('Uw browser wordt niet gesupport');
break;
case 'TooManyFiles':
$('#fileDropTarget').css('border', '2px dashed #FF0000').text('U kunt maar 1 foto tegelijk uploaden');
break;
case 'FileTooLarge':
$('#fileDropTarget').css('border', '2px dashed #FF0000').text('Uw foto is groter dan 10MB');
break;
case 'FileTypeNotAllowed':
$('#fileDropTarget').css('border', '2px dashed #FF0000').text('Alleen fotos zijn toegestaan');
break;
case 'FileExtensionNotAllowed':
$('#fileDropTarget').css('border', '2px dashed #FF0000').text('Alleen fotos zijn toegestaan');
break;
default:
$('#fileDropTarget').css('border', '2px dashed #FF0000').text(err);
break;
}
},
uploadStarted: fileUploadStarted,
uploadFinished: fileUploadFinished,
progressUpdated: fileUploadUpdate
});
</script>
But everytime I try to upload a file; I get the error: 'Not Found'. The other problem is that I want to upload these files with a asp.net control and a POST request. I dont know how to add a file to a FileUploadControl; and I have no idea how to get the file from the dragfield the upload control.
protected void Page_Load(object sender, EventArgs e)
{
string filetype = Request.QueryString["fileType"];
string filename = Request.QueryString["fileUpload"];
FileUpload FileUploadControl = new FileUpload();
string location = "~/upload";
try
{
if (filetype == "avatar") location = "images/profile/";
FileUploadControl.SaveAs(Server.MapPath("~/") + location + filename);
LabelStatus.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
LabelStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
Thank you in advanced.