How can I upload a file in an FTP server using javascript? - javascript

I want to upload a file to an FTP server.
I'm using Angular but after some research I realized that Angular has no way to upload files to FTP. So I tried to follow this code but I didn't have any success because 'new FtpConnection()' is not recognized by javascript.
Is there any other valid code to upload a file to FTP via javascript or do I need to upload the file to FTP via the backend? (I use c# in backend)

JavaScript has no native mechanisms for handling input and output, including FTP communication. It depends on the host environment to provide APIs for that.
Browsers (which I assume you are using given your angular tag) do not provide JS with an API for making FTP uploads, so you cannot.
(The question you reference is about JS running in Adobe Photoshop, not in a web browser).
The closest you could get would be to write a web service which you could send a file to via HTTP and then have it make the FTP upload.

Related

How to use openstack on client side

I try to make a upload/download services of files for my website, and i'm trying to use the object storage from openstack. The thing is, i have no problem doing it via php and openstack php sdk, but when i'm trying to do it via some javascript, i can't find a good sdk or methods.
I'm not using node, I have a php server, and a javascript client. I would like to uploads or download files directly from the javascript client. I don't want the file to transfer through the php server. I managed to create openstack tokens with the php sdk, maybe i could send those to the javascript so then they can authenticate? It's been one week of searching with no solutions...
Openstack has a S3 plugin that can ease your search for a library/sdk.
Otherwise, you should forge a temporary URL server-side, I'm sure your PHP library has tooling for this. The URL can then be used client-side to PUT the file.
The temporary URL is forged in a way to open temporary write-only access for the upload. There is also the same kind of URL to open read-only access to some elements.
So, either the client requests for a place to upload to the PHP which sends back the URL, or simply have the client upload the file to your PHP that will forge the link and then redirect the request to the URL.

Nodejs file download via client (client act as 'middleman' between server and file)

First off, I know this seems illogical when I could just send the download URL to the server. The issue with that is that user's can access these download links and so for those who can I need to be able to download it. I can't really explain why as I am under NDA.
I am trying to download a file from a URL via the client (browser) and stream the data directly to the server where the file is saved so the client essentially acts as a "middleman" and does not require the file to be downloaded to the client's machine.
I have been experimenting with "socket.io-stream" and "socket.io-file" but i am having a few issues with both. "socket.io-stream" allows me to upload a specific file from the client to the server but the uploaded file has a size of 0kb and doesn't have any examples on Github.
"socket.io-file" has examples, which I followed and currently have it setup so I can use an input tag to select a file to upload to the server successfully.
From what I can see the "socket.io-file" upload function takes a file object as the parameter.
So I have two questions really:
Is there a plugin for JS (Browser) & NodeJs (Server) that would allow me to do this?
or
How can I create a File Object from an external url?
I solved this is the end, using a chrome extension to download the file as a blob object, pass the object to the content script and then use socket.io-stream to upload it to the server.

How to check a file is existence from C# or Javascript?

We have a application hosted in some other server located in another country, I am going to use that application from different country, while using that application i want to check the user's machine local 'C' drive particular path is having that file is exist or not.
How to achieve this from C# or Javascript?
From the C#
byte[] contents = null;
if (File.Exists("C:\Program Files (x86)\Fruit\fruitList.txt"))
{
using (FileStream fileStream = File.OpenRead(scannedFile))
{
contents = new byte[fileStream.Length];
}
}
but that mentioned 'C' drive path is searching in IIS server only (where we hosted the application). But i want to check in client (user's) machine.
I couldn't find a code in Javascript.
Please help me, to achieve the expected result.
Thank you.
What you are attempting to do is not easily possible. JS will not work as it is blocked from accessing the user's client machine for security reasons. C# cannot access the client at all as it runs on the server.
The alternative is for the user to manually pick the required file from their machine using a <input type="file" /> then upload that file to your server for it to be processed and validated.
Failing that you could write a program which the user installs on their machine which provides methods for your website to retrieve information from the client - however this is an incredibly involved process.
You cant access the clients drive from the server side, would be a major security issue. You will have to ask the client to tell you if they have the file or not or find another work around. This is also whys its searching in IIS server, because it thinks you are looking for a local file. Hope this helps!
In a Website (C#) you will only have access to the files of the server because is the machine who is executing the application.
For Javascript who is execute in the client, that's not possible because client file access is not allowed by design, read this article:
Javascript Security
If you need this you have to develop an application to install in the client's PC and connect the server from this application, this is the only way, you will never have access to the client files since the browser.
Use this way
C#
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Get the file extension from fileStream variable
Javascript:
<input id="File" type="file"/>
$("#File")[0].files[0].name.split(".")

Upload local file from webbrowser directly to FTP server, bypass the web server

I want to upload user uploaded file (from website <form>) to my FTP server, in which I want to bypass the server and want that file should be sent to FTP server directly. Is it possible using PHP or JavaScript?
In current scenario when I upload file using HTML form and PHP to Apache server, the file is stored in /tmp/ directory and then I can transfer it to FTP location. But this takes double time in uploading as the filed is first uploaded to Apache server and then to the FTP server.
Cloud servers run this way, where Apache server can be by passed and file can be posted directly to cloud server)
I want this so that we can overcome the HTTP part and want to upload large file to FTP server without getting any HTTP upload restrictions.
The HTML form tag does not support the FTP.
You cannot use PHP either as it cannot access local (as of webbrowser) files.
So JavaScript is the only likely solution.
The XMLHttpRequest class theoretically supports FTP:
Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and ftp).
But in reality it probably does not.
See the accepted answer to What is the syntax to do a cross-domain XMLHTTPREQUEST to an FTP server?
So actually, there does not seem to be any solution readily available.
If possible, run a web server on the FTP server host and send the files using the web server (HTTP).
Another alternative is to stick with the transferring via your web server, but in a streaming mode. Post your file to the web server. Make the handling script continuously read the incoming data and have them continuously uploaded to the FTP server.
You won't save a bandwidth, but you will save time (as both transfers happen nearly in parallel).
Using the ftp_fput with a handle to the standard input should do (didn't try).
its very difficult to FTP data(BIGfile) to backup server without using HTTP protocol in a web application.
Lets say, S1-( Client Browser ), S2-(code container server), S3-(Files Backup server) and we want to upload 2gb file from s1 using FTP.
use case diagram This can be done by "JavaApplet" . we can embed uploader applet in webapplication. This applet will run inside browser sand box.
go through link sample code for ftp using applet
provided you have to enable java on your browser.sample code
ftp block diagram for side load

Reading files from hard drive with javascript

My application creates .xml files and stores them on the user's hard drive; there is a default folder that I set in the web.xml to store the files, let's say D:/temp. Now after I write the files I need to read them back with javascript, I am using a javascript library that has this function mxUtils.load('URL of the file') (this function returns the content of the file), the problem is that it is giving me an error Cross origin requests are only supported for HTTP, (I now it doesn't have anything to do with the function or the library) I think the problem is that you can't read local files because of some security issues. Anyone can advise me some solution? Thanks
You cant access local filesystem using javascript.
For accessing the file using javascript , you have to upload it to a server and access it using the files url.
As stated, you cannot access a file on the filesystem strictly through Javascript. You could, however, use the input file type to upload the file to your server and then read it:
<input type="file" name="myfileinput">
Then, you can access it via the $_FILES global in PHP - other languages also provide this functionality through other means. Please note, again, that there is absolutely no way to access a file that is on someone's filesystem with Javascript without their consent (i.e. using the file input type). That would be a huge security risk - imagine going to a page and having it wipe your whole D:/ drive.
It's best to expose your files via HTTP and use mxUtils.load("http://yoursite/static/yourfile.xml").
Search for static files on Apache HTTP Server HowTo. Setup Apache to serve your xml files, make sure that you can view xml file in browser and then use the same url in mxUtils.load call.

Categories