how make the file path dynamic [duplicate] - javascript

This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 6 years ago.
Hi I wrote the below Java code to email a file as attachement. It works fine if I provide a sample filepath. But, I want it to take the file path browsed by the user in the Front End
//code to attach the file
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart.setText(sb.toString());
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//String filename = filePath;
String filename = "C:/Users/S.Mandava/Documents/ContactusAction.java";
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart2);
msg.setContent(multipart);
System.out.println("Attaching the file");
Transport.send(msg);
Form example,
I want the system to take the file path uploaded in the below form when the user clicks on submit and the file should be sent as an attachment in an email
<form >
<input type ="file"/ id="multiple_files" name="multiple_files" multiple><br/>
<button>Submit</button>
</form>
How do I do it? I want to use JSP and JavaScript on the Front End and Java on the Back End. I am having difficulty in getting the file path.

As #Rishal_dev_singh said, search for examples, Google is your friend... What you do is an InputStream, you don't define a path

Related

Can javascript display text of any external file with any extension? [duplicate]

This question already has answers here:
Local file access with JavaScript
(14 answers)
How to read a text file containing html without using input type file in JavaScript?
(2 answers)
Closed 3 years ago.
Lets consider that I have a text file named text.txt. So, now I made a file named showtext.html in which I have to write javascript such that it can display the text present in text.txt. I assume it would be like :
Text ( text.txt )
Show me up !
HTML ( showtext.html )
<!DOCTYPE html>
<html>
<body>
<script>
var src = "text.txt";
document.write( value.get( src ) );
</body>
</html>
Output of showtext.html
Show me up !
In, the above function the value.get() gets and then writes the value of the text.txt. So, can javascript really do such ? How can I make the value.get() ? Will it support only .txt extension or can support any custom extension ? Or this can only be done with PHP ?
"Local" means "all client, no server" :) If you don't have a server, you can't use things like fetch() (as far as I know, security restrictions prevent that). But you can read a file from the local computer on the client as long as the user selects the file themselves. You can't just read any arbitrary file from the client's computer (again, for security reasons).
If you're okay with letting the user select the file, look into the FileReader API.
HTML:
<input type='file' id='fileSelector' />
JS:
const fileInput = document.getElementById('fileSelector');
fileInput.addEventListener('change', event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.addEventListener('load', readEvent => {
// In here, readEvent.target.result will have the text of the file for you to work with.
});
reader.readAsText(file);
});

How to force download a file with JavaScript? [duplicate]

This question already has answers here:
Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?
(3 answers)
Closed 8 years ago.
I have a jQuery ajax form where a user submits his email, once the form has been submitted, the user should get an automated download of a file, the form looks like this:
$(".email-form").submit(function(event) {
event.preventDefault();
var $form = $(this),
form_email = $form.find("input[name='email']").val(),
url = $form.attr("action");
var posting = $.post(url, { email: form_email });
posting.done(function() {
$(".popup-design").hide();
$("input[name='email']").val("");
$("#done").show();
});
});
and also I need to have to user automatically download a file of my choice when posting is done. How can I do that?
You will have to:
Create an iframe and add it to your document.
Set the iframe's SRC to be a URL to the download.
Have the server send the correct headers to cause a download prompt.
Edit - Found this question which is very much related
Using jQuery and iFrame to Download a File

How to get Folder & File name ONLY while Browsing with jQuery [duplicate]

This question already has answers here:
How do i get the full path of the file in Firefox
(1 answer)
How to get the file path from HTML input form in Firefox 3
(8 answers)
Closed 8 years ago.
So far I've been able to get only the file names from the browse button by using just jQuery and HTML file form. Here is my fiddle: http://jsfiddle.net/p42Wv/2/
MY ATTEMPT
$("#file").bind("change", function() {
var elem = document.getElementById("file");
var names = [];
for (var i = 0; i < elem.files.length; ++ i) {
names.push(elem.files[i].name);
}
var names2 = names.join('\n');
$("#filename").val( names2 );
});
I get the file names as I'm supposed to BUT how do i get the Folder Names as well as the file names when I select a mixed list of files and folders in the "Browse" button?
EDIT:
NOTE: I'm looking for Folder Name and NOT the Folder Path.
I've seen website such as Google Drive that lets user's select a WHOLE folder to upload. Isn't the mere extraction of folder names such an issue? If not, how does Google Drive for instance, access the subfiles of a folder and upload?
Thanks a lot,
John

How To get the full path of the folder selected [duplicate]

This question already has answers here:
How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
(14 answers)
Closed 9 years ago.
I have created <input type="file" name="files" id="fld1" webkitdirectory > in an html file.
i can get the files using
var imageFiles = document.getElementById("fld1"),
filesLength = imageFiles.files.length;
for (var i = 0; i < filesLength; i++) {
alert(imageFiles.files[i].name);
}
How can i get the full path of the directory selected in javascript . please help
I want to show all the image files present in any folder using jquery slider. i can get the name of the files present in that folder but i cannot get the full path of the folder. how can i get it.
i have done this to get the file path but it only worked in IE
<script language="JavaScript">
function GetDirectory() {
strFile = document.MyForm.MyFile.value;
intPos = strFile.lastIndexOf("\\");
strDirectory = strFile.substring(0, intPos);
alert(strFile + '\n\n' + strDirectory);
return false;
}
</script>
For obvious security reasons you cannot do that. Browsers won't allow you to access and explore the file system of the client computers using javascript. Some browsers will simply return some fake path to the file.
Check File API documentation, the File section gives details about the File object.
Quoting from the doc:
The name of the file; on getting, this must return the name of the
file as a string. There are numerous file name variations on different
systems; this is merely the name of the file, without path
information. On getting, if user agents cannot make this information
available, they must return the empty string.

How to use input type file in phonegap? [duplicate]

This question already has answers here:
How to upload file with Phonegap and JqueryMobile?
(2 answers)
Closed 3 years ago.
How to use html file attribute in phonegap ? so that i can browse any .txt file from my android device and upload it to the server??
I read file transfer method in phonegap documentation but according to that it is possible to upload files to the server only through url.But is it possible in normal way like :
<input type=file /><button>upload</button>
input type="file" accept="image/*" doesn't work in phone gap? i looked at this link but that says only for images .
But how to upload the text files ?
Any help??
Can anybody answer this?
You can't use input file on Phonegap. It's not supported. You need make something like this:
function uploadDatabase(callback) {
// file.entry accessible from global scope; see other tutorial
var filepath = file.entry.fullPath,
uploadOptions = new FileUploadOptions(),
transfer = new FileTransfer(),
endpoint = "http://facepath.com/payload";
uploadOptions.fileKey = "db";
uploadOptions.fileName = "database.db";
uploadOptions.mimeType = "text/plain";
transfer.upload(filepath, endpoint, transferComplete,
transferError, uploadOptions);
}
in mime type put the type of file
I wrote this answer "You can't use input file on Phonegap. It's not supported.". Check the solution on https://stackoverflow.com/a/13862151/1853864

Categories

Resources