How to pass a file to server and then send it back to client?
You can upload your files using multer. You can access the uploaded file from the destination folder on the server or local
const express = require("express");
const multer = require("multer");
const upload = multer({ dest: "uploads/" });
const app = express();
app.post("/upload", upload.single("productImage"), function (req, res, next) {
var filename = req.file.filename;
res.redirect("/public/uploads/" + filename);
});
upload.single('productImage') refers to the the input element name and you can get the file name on the backend with req.file.filename<form action="/upload" method="POST" enctype="multipart/form-data">
<input type='file' name="productImage" required/>
<button type="submit">Submit</button>
</form>
After that you can access the file on the front-end http://localhost:3000/uploads/<filename>
File transfer Client to Client in JAVA
I was able to make a transfer between two clients easily with the information provided and a little research on stackOverflow to understand more about out/inputStreams!
This post also helped me a lot: Sending a file with Java Sockets, losing data
next step is the shared transfer
Sending file from server to client using tcp
Check this:
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PORT 2024
extern int errno;
int main (){
struct sockaddr_in server;
struct sockaddr_in from;
int sd;
sd = socket (AF_INET, SOCK_STREAM, 0);
bzero (&server, sizeof (server));
bzero (&from, sizeof (from));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (PORT);
if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1){
perror ("bind(). error\n");
return errno;
}
if (listen (sd, 5) == -1){
perror ("listen(). error\n");
return errno;
}
while (1){
int client;
int length = sizeof (from);
printf ("connect to port : %d...\n",PORT);
fflush (stdout);
client = accept (sd, (struct sockaddr *) &from, &length);
if (client < 0)
continue;
char file[100];
read (client, file, 100) <= 0;
printf("Filename : %s\n\n", file);
//
FILE *fl = fopen(file, "r");
char text[500]; //your file size
int len = 0;
char c;
while((c = fgetc(fl)) != EOF){
text[len] = c;
len++;
}
text[len] = '\0';
//printf("%d", len);
//printf("%s", text);
write(client, &len, 4);
write(client, text, len);
close (client);
}
}
client.c#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
extern int errno;
int port;
int main (int argc, char *argv[]){
int sd;
struct sockaddr_in server;
if (argc != 3){
printf ("%s <server_adress> <port>\n", argv[0]);
return -1;
}
port = atoi (argv[2]);
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
perror ("socket() error.\n");
return errno;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons (port);
if (connect (sd, (struct sockaddr *) &server,sizeof (struct sockaddr)) == -1){
perror ("connect(). error\n");
return errno;
}
char file[100];
printf("Give me filename: ");
fflush(stdout);
scanf("%s", file);
if (write (sd, file, 100) <= 0)
{
perror ("[client]Eroare la write() spre server.\n");
return errno;
}
int len;
char text[500];
read(sd, &len, 4);
read(sd, text, len);
//printf("%s\n", text);
FILE *fl = fopen("newFile.txt", "w");
fprintf(fl, "%s", text);
fclose(fl);
close (sd);
}
Here the client is giving a file name to server, it ( the server ) opens the file and reads all the content from it, sends the data to client who creates a new file and writes data into new created file.UPD.
concurentServer.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#define PORT 2908
extern int errno;
typedef struct thData{
int idThread;
int cl;
}thData;
static void *treat(void *);
void answer(void *);
int main (){
struct sockaddr_in server;
struct sockaddr_in from;
int nr;
int sd;
int pid;
pthread_t th[100];
int i=0;
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
perror ("[server]socket().\n");
return errno;
}
int on=1;
setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
bzero (&server, sizeof (server));
bzero (&from, sizeof (from));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (PORT);
if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1) {
perror ("[server]bind().\n");
return errno;
}
if (listen (sd, 2) == -1){
perror ("[server]listen().\n");
return errno;
}
while (1){
int client;
thData * td;
int length = sizeof (from);
printf ("[server]Port : %d...\n",PORT);
fflush (stdout);
if ( (client = accept (sd, (struct sockaddr *) &from, &length)) < 0){
perror ("[server]accept().\n");
continue;
}
int idThread;
int cl;
td=(struct thData*)malloc(sizeof(struct thData));
td->idThread=i++;
td->cl=client;
pthread_create(&th[i], NULL, &treat, td);
}
};
static void *treat(void * arg)
{
struct thData tdL;
tdL= *((struct thData*)arg);
printf ("[thread]- %d - Waiting for message...\n", tdL.idThread);
fflush (stdout);
pthread_detach(pthread_self());
answer((struct thData*)arg);
close ((intptr_t)arg);
return(NULL);
};
void answer(void *arg){
char file[100];
int ap;
struct thData tdL;
tdL= *((struct thData*)arg);
//
if (read (tdL.cl, file, 100) <= 0){
printf("[Thread number : %d]\n",tdL.idThread);
}
FILE *fl = fopen(file, "r");
char text[500]; //your file size
int len = 0;
char c;
while((c = fgetc(fl)) != EOF){
text[len] = c;
len++;
}
text[len] = '\0';
//printf("%d", len);
//printf("%s", text);
write(tdL.cl, &len, 4);
write(tdL.cl, text, len);
}
Node.js send file to client
You need to set some header flags;
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size,
'Content-Disposition': 'attachment; filename=your_file_name'
});
For replacing streaming with download;var file = fs.readFile(filePath, 'binary');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'audio/mpeg');
res.setHeader('Content-Disposition', 'attachment; filename=your_file_name');
res.write(file, 'binary');
res.end();
Sending files from a client to a server and then reading the file in another client using sockets in C
Server side:
send_file(clients[i]->uid);
ought to besend_file(clients[i]->sockfd);
fclose(f);
should probably go beforesend_message(buff_out, cli->uid);
printf("%s\n",ptr);
(beforeif(bytes_received==-1)
) ought to be removed
sending file to client for download
Try this -
window.open('/api/download/getFile?file=yourfile');
Send file to client and delete it
This snippet should do the trick, but notice that this will result in loading the whole file into the memory (of the server).
private static void DownloadFile(string path)
{
FileInfo file = new FileInfo(path);
byte[] fileConent = File.ReadAllBytes(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", file.Name));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(fileConent);
file.Delete();
HttpContext.Current.Response.End();
}
Related Topics
What Is the Js Equivalent to the PHP Function Number_Format
Symfony 2.7 Cache:Clear Command Checks Every Database Connection
How to Select a MySQL Database to Use with Pdo in PHP
PHP Error: Cannot Use Object of Type Stdclass as Array (Array and Object Issues)
What Is the Best Method for Getting a Database Connection/Object into a Function in PHP
Why Doesn't PHP Permit Private Const
How to Increase by 1 All Keys in an Array
Print Directly to Network Printer Using PHP
How to Encrypt HTML Source Code Output Using PHP
Woocommerce Cart Quantity Base Discount
File_Get_Contents('Php://Input') Always Returns an Empty String
Image Upload from iPhone Strips Exif Data
Type Hinting: Default Parameters
How to Convert Ldap Timestamp to Unix Timestamp
Find Array Key in Objects Array Given an Attribute Value