How to let user specify location of file from Wakanda app - wakanda

I am trying to let a user select a text file (via a standard file dialog) in a Wakanda web app and read it into a variable. Is there a built-in Wakanda client side call to do this or a jquery way?

You can do it with pure HTML.
Example 1
The simple file selector is like this:
<input type=file name=varname>
Example 2
If you actually want to read the file on the client side into a variable then the HTML5 FileReader should help:
document.getElementById('file').addEventListener('change', readFile, false);
function readFile (evt) {
var files = evt.target.files;
var file = files[0];
var reader = new FileReader();
reader.onload = function() {
var output = this.result;
document.getElementById('test').innerHTML = output;
}
reader.readAsText(file)
}
<input type="file" id="file" name="file" enctype="multipart/form-data" />
<br />
<br />output:<br />
<span id=test></span>
Example 2 is a slightly modified version of this answer

Related

Wordpress Fetch API Admin Ajax 400 Errors

When I submit my form to my wp-admin/admin-ajax.php I get a 400 error using fetch.
I have tried removing content type.
Hard coding the Fetch URL
using FormData
let formData = new FormData(form);
Using URLSearchParams
using them both
const data = new URLSearchParams(new FormData(form));
I cant get it to work and am out of answers online so thought I'd ask here.
Can you see an error in my code?
<form >
<input type="text" name="fname">
<input type="text" name="email">
<input type="hidden" name="action" value="add_user_details_hook">
<button type="submit">Submit</button>
</form>
<script>
let form = document.forms[0];
const data = new URLSearchParams(new FormData(form));
form.addEventListener('submit', function(e){
e.preventDefault();
fetch('/wp-admin/admin-ajax.php', {
method: 'post',
body: data
})
ADMIN-AJAX.php
function ajax_add_user_details_func(){
var_dump('hey!');
}
add_action('wp_ajax_add_user_details_hook', 'ajax_add_user_details_func');
add_action('wp_ajax_nopriv_add_user_details_hook', 'ajax_add_user_details_func');
Edit I do believe it has something to do with empty FormData as even when I iterate over the object with forloop the fields are empty:
Example:
for (var [key, value] of formData.entries()) {
console.log(key, value);
}
Returns no values

How to upload files in asp.net 3.1 MVC with AJAX

I'm new to asp.netcore 3.1, and now I'm learning how to upload file using AJAX in MVC, so I have tried the code that I got and here the code
in view cs.html `Upload one or more files using this form:
</p>
<input type="file" name="files" multiple />
</div></div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Upload" />
</div></div>
</form>`
In my controller:
[HttpPost("FileUpload")]
public async Task<IActionResult> Index(List<IFormFile> files) {
long size = files.Sum(f => f.Length);
var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
var filePaths = new List<string>();
foreach (var formFile in files) {
if (formFile.Length > 0) {
// full path to file in temp location
var filePath = Path.Combine(basePath, formFile.FileName);
filePaths.Add(filePath);
using (var stream = new FileStream(filePath, FileMode.Create)) {
await formFile.CopyToAsync(stream);
}
}
}
// process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok(new { count = files.Count, size, filePaths });
}
I tried to change this:
asp-controller="FileUpload" asp-action="Index">
The code above was made so that the action directly goes to the directed controller, but I want to change so it would pass the file to AJAX first.

autocapitalize="words" broken on mobile Safari in iOS 8,9 - Is there a workaround?

The <input> attribute autocapitalize="words" is broken in mobile Safari under iOS 8,9 with the default iOS keyboard. It uppercases the first 2 letters of the field, not the first letter of each word.
Official documentation says is supported: https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/Attributes.html
To test, open the following field on iOS emulator or real device:
First name: <input type="text" autocorrect="off" autocapitalize="words" value="First Name">
You can use https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit to test, or this snippet on iOS 8 or 9:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test autocapitalize</title>
</head>
<body>
<form>
<label for="words">autocapitalize="words"</label>
<input type="text" autocapitalize="words" name="text1" id="words" /><br />
<label for="sentences">autocapitalize="sentences"</label>
<input type="text" autocapitalize="sentences" name="text2" id="sentences" /><br />
<label for="none">autocapitalize="none"</label>
<input type="text" autocapitalize="none" name="text3" id="none" />
</form>
</body>
</html>
I'm amazed this has been present since 8.x and has passed under the radar.
Is there a known workaround?
Update 10/13:
iPhone 6s+ Safari completely ignores any HTML attribute set on the input field.
There seems to be a workaround for this issue if you are willing to (temporarily) include this library: https://github.com/agrublev/autocapitalize. It does however require jQuery, so might not be ideal on a mobile device. I've created this small piece of code which does the same thing just for words without the use of jQuery. It can ofcourse be extented to also include other cases.
The example below also capitalizes the words initially on page ready, instead of just on the 'keyup' event. I've tested the code on several devices and haven't gotten an error. But feel free to comment if something doesn't work or you feel something can be done better.
Note that the 'domReady' function I added works for IE9 and up. Please see this if you require support for an older version.
// Create one global variable
var lib = {};
(function ( lib ) {
lib.autocapitalize_element = function (element) {
var val = element.value.toLowerCase();
var split_identifier = " ";
var split = val.split(split_identifier);
for (var i = 0; i < split.length; i ++) {
var v = split[i];
if ( v.length ) {
split[i] = v.charAt(0).toUpperCase() + v.substring(1);
}
};
val = split.join(split_identifier);
element.value = val;
}
lib.autocapitalize_helper = function(element) {
element.onkeyup = function(e) {
var inp = String.fromCharCode(e.keyCode);
if (/[a-zA-Z0-9-_ ]/.test(inp)) {
lib.autocapitalize_element(element);
}
};
}
lib.autocapitalize = function() {
var elements = document.querySelectorAll("input[autocapitalize], textarea[autocapitalize]");
for(var i = 0; i < elements.length; i++) {
lib.autocapitalize_helper(elements[i]);
lib.autocapitalize_element(elements[i]);
}
}
lib.domReady = function(callback) {
document.readyState === "interactive" || document.readyState === "complete" ? callback() : document.addEventListener("DOMContentLoaded", callback);
};
}( lib ));
// This function gets called when the dom is ready. I've added it to the lib variable now because I dislike adding global variables, but you can put it anywhere you like.
lib.domReady(function() {
lib.autocapitalize();
});

How to make Dojo as generic while submitting a form?

I have the following dojo (ver 1.9) code:
require(["dojo/dom", "dojo/on", "dojo/request", "dojo/dom-form"],
function(dom, on, request, domForm){
var form = dom.byId('user_login');
var selectedTabId = showIdOfSelectedTab();
// Attach the onsubmit event handler of the form
on(form, "submit", function(evt){
// prevent the page from navigating after submit
evt.stopPropagation();
evt.preventDefault();
// Post the data to the server
request.post("login1.php", {
// Send the username and password
data: domForm.toObject("user_login"),
// Wait 2 seconds for a response
timeout: 2000
}).then(function(response) {
dom.byId(selectedTabId).innerHTML = response;
});
});
}
);
And html below:
<form name="user_login" id="user_login">
User name: <input type="text" name="user_name" id="user_name" /><br />
Password: <input type="password" name="user_password" id="user_password" /><br />
<button id="submitbutton" name="submitbutton">Submit</button>
</form>
I want to make the above dojo code as generic by sending the post action (login1.php) and the form id (i.e., user_login). I tried several ways but I could not achieve it.
Please let me know if any of you have idea.
Thanks in advance.
-Uday
This is the demo drom the dojo Tutorial right?
http://dojotoolkit.org/documentation/tutorials/1.9/ajax/
Did you get any Errormessages?
So let's see.
Have you load the dojo libary correct? If not, the widgets can't be loaded.
Must be somthing like:
src="//ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js">
Check the path to the login1.php.
If it's in another Folder than your code the path must be something like "../myfolder /myphp/login1.php"
Regards, Miriam

load data into file upload MVC3

Html code:
#using (Html.BeginForm("Edit", "RoomInfo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" class="multi" />
<p>
<input type="submit" value="Update" />
</p>
}
Controller code;
public ActionResult Search(string id)
{
var arrayid = id.Split(',');
int roomid = int.Parse(arrayid[0]);
ViewBag.ImageID = new SelectList(db.RoomImgs.Where(p => p.RoomID == roomid), "ImageID", "FilePath");
return View(roominformation);
}
I'm trying to upload the image into server and insert the file path into database and I successful to insert in to server and database, but now I need to select out the file path and put into the file upload tag in Edit mode. I trying some method but still fail to set in. How can I do it?
If what you're trying to do is display the previously uploaded image and then allow users to modify the image.
You can't populate the file input like you would a text box. What most websites do is display the image above the input and then have the input box below if you want to change the image. If you want to remove the image then you could also have a checkbox for removing the image. Then on the server side you have to check if the checkbox is checked and clear the image or, if the file input is populated store the new image and update the database.

Resources