0

I searched the web to see if there was a way to send files (photos, videos, messages) to a user using an app. I couldn't find any clear examples or explanations on how to achieve it.

I have no idea, how to do this. Can anyone tell me how this can be done? Any help will be appreciated. I should also probably mention that the ultimate goal of this app is a messaging application.

2 Answers2

0

Not sure what you mean by "sending files" to a user, but if you want them to download a file using javascript, you could use this excellent jQuery plugin: http://jqueryfiledownload.apphb.com/

Of course, there are many alternatives, such as creating an invisible iframe and then loading the url to the file you want your users to download into that iframe. You can read more about this over here: Download File Using Javascript/jQuery

Hope it helps!

Community
  • 1
  • 1
Laurens Swart
  • 1,234
  • 9
  • 24
  • Thanks for the help, I'll go check it out. What I meant by sending a file is basically; let's say the user takes a photo from his camera, the photo is automatically saved into a folder so that the computer can access that photo (basically the photo is what I called a file in the question, same goes for a message and a video.). – Theodore Tremblot Feb 19 '17 at 14:10
  • Javascript doesn't have access to the user's local file system. I also am not aware of any javascript that accesses a phone's camera. I'm not entirely sure javascript would be the best for what you're trying to achieve (making a camera/messaging app??) – Laurens Swart Feb 19 '17 at 14:16
  • @LaurensSwart with html5, javascript has sandboxed access to the user's local file system – niceman Feb 19 '17 at 14:38
  • getUserMedia() accesses the devices media (so camera and audio). If you have any suggestions on how code could access a folder on the users device and display a file from the folder on a user that the user who took the photo wanted to have access to, I'm open to suggestions. And yeah, the end goal of the app is to take a photo and share it certain users. – Theodore Tremblot Feb 19 '17 at 14:39
  • @TheodoreTremblot as I said "sandboxed", you can't just access whatever file you like, javascript file api works this way : browsers present you a folder just for you(doesn't matter where this folder really is), you take a file as input from user and write that file to the directory the browser gave you, after that you can read those files whenever you need them, ofcourse the file could also come from the server not just the user – niceman Feb 19 '17 at 14:43
  • @TheodoreTremblot for your usecase, you don't need to access the user's file system, you can take the photo from the camera directly and send it to the other user via websockets – niceman Feb 19 '17 at 14:44
  • @niceman ok, how could I send it to him with websockets, is there a link I could search to learn more about them? – Theodore Tremblot Feb 19 '17 at 14:48
0

First let me tell you that you can use Kandy js sdk, you can find about it here : https://developer.kandy.io/, I didn't test it but it looks very promising and provides many of the features you need.

In case you want to do it on your own(or you're just curious), WebSockets is the main gamer here.

Http protocol as you should know by now is a request-response protocol, the client make a request to the server and the server responds.

But sometimes we want the server to talk to the client in which we can use SSE(Server-Sent events), in other times we want to create a connection between the server and the client and we want both to be able to send to it and receive from it, that's what WebSockets are made for.

In your usecase you could have two strategies : peer-to-peer or centralized, in centralized strategy when user X wants to send something to user Y it has to first send it to the server and the server sends it to user Y.

For this you need websockets, you create a connection between user X and the server and another between the Server and user Y, you send the file from user X to the connection, the server then sends it through the other connection to user Y, user Y can send a new file to the Server so that the Server sends it to user X, and so on so goes.

From this you can conclude that Websockets has server part and client part, normally we launch websockets server that is different than the web server serving the application(both can be on the same Server computer or in different computers), if you don't want to bring your own server you can look at pusher : https://pusher.com/.

Pusher is a cloud service that provide sdks for many languages including javascript, in case you want to have your own server I can't talk about the server-side part of your problem because your question is a javascript question, for the client-part have a look at socket.io, this provides the client part as well as node server part but you can use the client part in case you don't want to use node for the server.

In case you want to build peer-to-peer connections, you can look at simple-peer.

niceman
  • 2,653
  • 29
  • 57