<%
* VSAddinComment:SourceFile="~/../../fox/samples/wwdemo/wwdemo.prg"pcPageTitle = "File Upload Sample - West Wind Web Connection"
%><%Layout="~/views/_layoutpage.wcs" %><!-- remove sections if you're not using them --><%section="headers" %><style>.sample.btn {
min-width: 185px;
margin-top: 5px;
}
/* Container*/.fileUpload {
position: relative;
overflow: hidden;
}
/* hide the actual file upload control by making it invisible */.fileUploadinput.upload {
position: absolute;
top: 0;
right: 0;
margin: 0;
padding: 0;
font-size: 20px;
cursor: pointer;
opacity: 0;
filter: alpha(opacity=0);
}
#ImageListimg {
max-height: 160px;
float: left;
margin-left: 8px;
margin-bottom: 8px;
}
.sample {
padding-bottom: 25px;
}
</style><scriptsrc="~/lib/jquery/dist/jquery.min.js"></script><%endsection %><divclass="container"><divclass="page-header-text"><iclass="fa fa-list-alt"></i>
File Upload Sample
</div><divclass="btn-group margin-bottom-2x"><ahref="~/"class="btn btn-outline-primary btn-sm"><iclass="fa fa-home"></i> Samples</a><ahref="UppyAndCarousel.wwd"class="btn btn-outline-primary btn-sm"><iclass="fa fa-images"></i> Image Carousel Sample</a><ahref="showcode.wwd"class="btn btn-outline-primary btn-sm"><iclass="fa fa-code"></i> Server Code</a><ahref="showfileastext.wwd?file=fileupload.wwd"class="btn btn-outline-primary btn-sm"><iclass="fa fa-code"></i> Script Code</a><ahref=""class="btn btn-outline-primary btn-sm"><iclass="fa fa-refresh"></i> Refresh</a></div><divid="ImageList"><%=pcImageListHtml %></div><divclass="clearfix margin-bottom"></div><!-- CLASSIC UPLOAD EXAMPLE --><divclass="container sample"><h3>Standard HTML Form Upload</h3><p>
This example uses a classic HTML server form submission to upload
one or multiple selected image files to the server. It uses the new
<code>Request.MultipartFiles()</code> function to retrieve a collection
of files on the server.
</p><formaction="FileUpload.wwd"method="post"enctype="multipart/form-data"><!-- <form action="FileUpload.wwd" method="post" enctype="multipart/form-data"> --><!-- wrap the file input into a div so we can style the button --><divclass="fileUpload btn btn-primary"><span><iclass="fa fa-image"></i>
Select images
</span><inputtype="file"id="upload"name="upload"class="upload"multipleaccept="image/*" /></div><buttontype="submit"class="btn btn-primary"><iclass="fa fa-upload"></i>
Upload...
</button><divid="filename"></div><!-- capture the filename to upload and display --><script>document.getElementById("upload").onchange = function () {
var output = this.files.length + " file(s):\r\n";
for (var i = 0; i < this.files.length; i++) {
output += this.files[i].name + "\r\n";
}
document.getElementById("filename").innerText = output;
};
</script></form></div><!-- CLASSIC UPLOAD EXAMPLE --><!-- AJAX UPLOAD EXAMPLE --><divclass="container sample"><h3>Ajax File Upload</h3><p>
This example uses plain AJAX and HTML5 to asynchronously upload one or more image files
using script code to capture the file stream of the selected files and posting them
to the server using jQuery's <code>.ajax()</code> method. Provides a generically reusable
function to handle the upload.
</p><formid="ajaxForm"name="ajaxForm"><!-- wrap the file input into a div so we can style the button --><divclass="fileUpload btn btn-primary"><span><iclass="fa fa-upload"></i>
Upload Images
<iid="ajaxProgress"class="fa fa-spinner fa-spin"style="display: none"></i></span><inputtype="file"id="ajaxUpload"name="ajaxUpload"class="upload"accept="image/*"multiple /></div><imgid="uploadedImage"style="display: none; max-width: 95%; margin: 15px 0" /></form></div><script>// Catch the file selectionvar files = null;
$("#ajaxUpload").change(function (event) {
files = event.target.files;
// no further DOM processing
event.stopPropagation();
event.preventDefault();
// show spinner
$("#ajaxProgress").show();
uploadFiles("Upload",
"FileUpload.wwd?type=ajax",
functionuploadSuccess(data, textStatus, jqXHR) {
var files = data;
$("#ajaxProgress").hide();
if (data.error)
toastr.error("Upload failed");
toastr.success("Upload completed.");
var images = "";
for (var i = 0; i < files.length; i++) {
var file = files[i];
images += '<img src="../temp/' + file.filename + '" />';
}
$("#ImageList").html(images);
},
functionuploadError(jqXHR, textStatus, errorThrown) {
$("#ajaxProgress").hide();
toastr.error("Upload failed.");
});
});
/*
Generic re-usable Ajax Upload function
Pass in from Change event of file upload control
fileId - Id of the file Form vars
file results in (file0, file1, file2 etc)
url - Url to upload to
success - success handler
data object of JSON result data from server
error - error handler
*/functionuploadFiles(fileId, uploadUrl, success, error) {
// Create a formdata object and add the filesvar data = new FormData();
$.each(files, function (key, value) {
data.append(fileId, value);
});
$.ajax({
url: uploadUrl,
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false!
success: success,
error: error
});
}
</script><!-- END AJAX UPLOAD EXAMPLE --><!-- Single Button plUpload Example --><divclass="container sample"><h3>Uppy UI</h3><p>
This example uses the third party uppy uploader and UI component
to upload images using an interactive UI. Files are uploaded
asynchronously and the UI provides some progress information.
</p><buttonclass="btn btn-primary"id="UppyImageUploader"><iclass="fa fa-picture-o"style="color: limegreen"></i><iclass="fas fa-upload"></i>
Uppy Uploader Popup UI
</button></div><linkhref="~/lib/uppy/dist/uppy.min.css"rel="stylesheet"><script>//IE 10+ support via Polyfill for Promisesif (typeofPromise !== "function") {
document.write(
"<script src='https:\/\/cdn.jsdelivr.net\/npm\/es6-promise@4\/dist/es6-promise.min.js'><\/script>");
document.write(
"<script src='https:\/\/cdn.jsdelivr.net\/npm\/es6-promise@4\/dist\/es6-promise.auto.min.js'><\/script>");
}
</script><scriptsrc="~/lib/uppy/dist/uppy.min.js"></script><script>// global passed into upload script
_uploader = {
uploadUrl: "<%= Process.ResolveUrl([~/UppyUpload.wwd?id=entryId]) %>"
};
var uppy = Uppy.Core({
debug: true,
autoProceed: true,
restrictions: {
allowedFileTypes: ["image/jpeg", "image/png", "image/gif"],
maxNumberOfFiles: 8
}
})
.use(Uppy.Dashboard,
{
trigger: '#UppyImageUploader',
proudlyDisplayPoweredByUppy: false,
inline: false,
width: 550,
height: 350,
disableStatusBar: false,
disableInformer: false,
locale: {
strings: {
browse: "Browse",
dropPasteImport:
'%{browse}, drop or paste files here, or import from one of the locations above. ' +
'Please use images in Landscape (wide) format.'
}
}
})
.use(Uppy.Webcam,
{
target: Uppy.Dashboard,
modes: [
'picture'
],
locale: {
strings: { smile: "take a picture" }
},
countdown: false,
facingMode: "environment"
})
.use(Uppy.XHRUpload,
{
endpoint: _uploader.uploadUrl,
formData: true
});
// All files completed - close dialog
uppy.on('complete',
function () {
setTimeout(function () {
uppy.getPlugin('Dashboard').closeModal();
uppy.reset();
},
1000);
});
// Add Meta Data to the File Upload - EntryId
uppy.on('file-added',
function (file) {
uppy.setFileMeta(file.id,
{
entryId: "ReferenceId"
});
});
// Individual file upload completed
uppy.on('upload-success',
(file, body) => {
var fileEx = uppy.getFile(file.id);
var response = fileEx.response;
var result = response.body;
var imageId = result.imageId;
console.log("file uploaded: " + file.id + " - " + result);
var image = '<img src="' + result.imageUrl + '" />';
$("#ImageList").append(image);
return;
///var img = $("<img>").prop("src", result.imageUrl);
var div = $('<div class="image-container " style="background: #444;margin: 5px;">');
var divImageContainer =
'<div class="image-icon-container">' +
'<div class="image-rotateleft-icon" data-id="' +
imageId +
'" title="Rotate Left"><i class="fa fa-rotate-left"></i></div>' +
'<div class="image-rotateright-icon" data-id="' +
imageId +
'" title="Rotate Right"><i class="fa fa-rotate-right"></i></div>' +
'<div class="image-delete-icon" data-id="' +
imageId +
'" title="Remove Image"><i class="fa fa-remove" style="color: red"></i></div>' +
'</div>';
div.append(divImageContainer)
.append(img);
// add item to the slider
$("#Slider").slick("slickAdd", div);
setTimeout(function () {
// move to the new slidevar slideCount = $("#Slider")[0].slick.slideCount;
$("#Slider").slick("slickGoTo", slideCount - 1);
},
100);
});
</script><!-- End Uppy --></div><!-- end #MainView.container --><%section="scripts" %><scriptsrc="~/lib/bootstrap/dist/js/bootstrap.min.js"></script><linkhref="~/lib/toastr/build/toastr.min.css"rel="stylesheet" /><scriptsrc="~/lib/toastr/build/toastr.min.js"></script><script>
toastr.options.positionClass = 'toast-bottom-right';
</script><%endsection %>