如何将图像和视频上传到Web服务器?

时间:2018-02-09 07:44:51

标签: c# .net ftpwebrequest

我有以下代码。上传成功但图像和视频已损坏。我认为问题是因为编码设置为UTF8。如何在C#中将图像和视频上传到Web服务器?

private static void Upload(string ftpFilePath, string file)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFilePath);

        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(Properties.Settings.Default.FtpUser, Properties.Settings.Default.FtpPass);

        using (Stream fileStream = File.OpenRead(file))
        using (Stream ftpStream = request.GetRequestStream())
        {
            fileStream.CopyTo(ftpStream);
        }

        FtpWebResponse response = null;

        try
        {
            response = (FtpWebResponse)request.GetResponse();

            // Output message on screen
            Log.Write($"{request.Method} {ftpFilePath}", Log.Status.UPLOADED);
        }
        catch (Exception ex)
        {
            // Output message on screen
            Log.Write($"FTP UPLOAD: {response.StatusCode} - {ex.Message}", Log.Status.ERROR);
        }
    }

0 个答案:

没有答案