[
https://issues.apache.org/jira/browse/WICKET-7033?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17711341#comment-17711341
]
ASF GitHub Bot commented on WICKET-7033:
----------------------------------------
reiern70 commented on code in PR #571:
URL: https://github.com/apache/wicket/pull/571#discussion_r1164023647
##########
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FileUploadToResourceField.js:
##########
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+;(function (undefined) {
+
+ 'use strict';
+
+ if (typeof(Wicket.FileUploadToResourceField) === 'object') {
+ return;
+ }
+
+ Wicket.FileUploadToResourceField = function (settings,
clientSideSuccessCallBack, clientSideCancelCallBack, connectionErrorCallBack)
+ {
+ this.inputName = settings.inputName;
+ this.input = document.getElementById(this.inputName);
+ this.resourceUrl = settings.resourceUrl + "?uploadId=" +
this.inputName;
+ this.ajaxCallBackUrl = settings.ajaxCallBackUrl;
+ this.clientSideSuccessCallBack = clientSideSuccessCallBack;
+ this.clientSideCancelCallBack = clientSideCancelCallBack;
+ this.connectionErrorCallBack = connectionErrorCallBack;
+ }
+
+ Wicket.FileUploadToResourceField.prototype.upload = function()
+ {
+ // we add the files to a FormData object.
+ var formData = new FormData();
+ var totalfiles = this.input.files.length;
+ for (var index = 0; index < totalfiles; index++) {
+ formData.append("FILE-UPLOAD",this.input.files[index]);
+ }
+ // pass the input name to knw where to store files at server side.
+ formData.append("uploadId", this.inputName);
+ var self = this;
+ // we use jQuery to post the file to the resource (this.resourceUrl)
+ // and we keep a reference to the request in order to be able
+ // to cancel the upload
+ this.xhr = $.ajax({
+ url: this.resourceUrl,
+ type: "POST",
+ data: formData,
+ processData: false,
+ contentType: false,
+ success: function (res) {
+ // do clean up on success
+ self.clientSideSuccessCallBack();
+ if (res.error) {
+ var ep = {'success': false, 'errorMessage':
res.errorMessage};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ self.connectionErrorCallBack(res);
+ } else {
+ var ep = {'success': true, 'filesInfo':
JSON.stringify(res)};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ }
+ },
+ error: function (jqXHR, textStatus, errorThrown) {
+ if (textStatus === "abort") {
+ // user aborted the upload.
+ self.clientSideCancelCallBack();
+ var ep = {'success': false, 'errorMessage':
'upload.canceled'};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ } else if (textStatus === "error"){
+ var ep = {'success': false, 'errorMessage': errorThrown};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ self.connectionErrorCallBack();
+ } else if (textStatus === "parsererror"){
+ // this error will only happen is generated JSON at server
side is faulty
+ var data = jqXHR.responseText;
+ Wicket.Log.log(data);
+ }
+ }
+ });
+ }
+
+ // cancel the upload
+ Wicket.FileUploadToResourceField.prototype.cancel = function () {
+ // we have a reference to the request we can cancel it.
+ if (this.xhr) {
+ this.xhr.abort();
+ Wicket.Log.log("upload canceled!");
+ delete (this.xhr);
+ } else {
+ Wicket.Log.log("Too late to cancel: upload already finished.");
+ }
Review Comment:
Done.
> add support to uploading to a resource
> --------------------------------------
>
> Key: WICKET-7033
> URL: https://issues.apache.org/jira/browse/WICKET-7033
> Project: Wicket
> Issue Type: New Feature
> Components: wicket
> Reporter: Ernesto Reinaldo Barreiro
> Assignee: Ernesto Reinaldo Barreiro
> Priority: Major
> Fix For: 10.0.0, 9.14.0
>
>
> Add support for the following:
> * Upload to a resource in an asynchronous non page blocking request
> * Add an optional way to block the user from leaving the page while the
> upload is happening
> * Ways to cancel the upload
> * Adapt the upload progress bar to work with this new "component" and improve
> its code as in some corner cases it is producing client side errors (I
> created an issue for that some time ago).
> * Maybe useful too: create a web socket based progress bar, as the upload
> progress bar now works pulling the server every second.
> * Also to add an example to wicket-examples that uses a smart JS uploader,
> like in the blog
> (https://github.com/martin-g/blogs/blob/master/file-upload/). This way you
> will verify that the new APIs are easily extendable.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)