简体   繁体   中英

How to upload an image to any web server, local or remote which my application has permissions to

My first decision was whether to store images in a database or file system and after some research I have chosen to do file system.

I need this to scale horizontally by acquiring more web servers as images increase so I will need to store the network/internet location of an image somewhere so I can get to it whatever web server it may be on.

I have what I need in theory but I do not know how to take a file and save it on a remote server. Specifically what is the best way to do this? Are UNC paths an option?

Just to be clear, I have already got the image server side, just need to send it/save it to any web server of my choice.

Locally you can just File.Copy() . Otherwise you have to upload them using FTP:

public void UpLoadFile(String serverFilePath, string localFilePath)
{
    String serverFullPath = "ftp://" + s_ServerHost + serverFilePath;
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(serverFullPath);
    ftp.Credentials = new NetworkCredential("user", "password");
    ftp.KeepAlive = true;
    ftp.Method = WebRequestMethods.Ftp.UploadFile;
    ftp.UseBinary = true;

    using (FileStream fs = File.OpenRead(localFilePath))
    {
        Byte[] buffer = new Byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
    }

    using (Stream ftpStream = ftp.GetRequestStream())
        ftpStream.Write(buffer, 0, buffer.Length);
}

In order to retrieve it, you have to know the IP/hostname of your server and the final public path of your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM