如何在没有ftp的情况下将文件复制到c#中的远程服务器 [英] how to copy files to a remote server in c# without ftp

查看:75
本文介绍了如何在没有ftp的情况下将文件复制到c#中的远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有远程服务器的用户名和密码,如何自动复制文件并执行c#

I have username and password of a remote server , how to automatically copy files and execute with c#

推荐答案

这是



客户代码

This is the

CLIENT CODE
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string fileToUpload = Server.MapPath("~/Files/Ricky_Martin_Livin_la.mp3");
        string uploadUrl = "http://localhost/soundcheck/uploadfiles.aspx";
        //string uploadUrl = "http://10.0.2.2/musicapp/handle_upload.php";
        FileStream rdr = new FileStream(fileToUpload, FileMode.Open);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
        req.Method = "PUT"; // you might use "POST"
        req.ContentLength = rdr.Length;
        req.AllowWriteStreamBuffering = true;

        Stream reqStream = req.GetRequestStream();

        byte[] inData = new byte[rdr.Length];

        // Get data from upload file to inData 
        int bytesRead = rdr.Read(inData, 0, int.Parse(rdr.Length.ToString()));
        // put data into request stream
        reqStream.Write(inData, 0, int.Parse(rdr.Length.ToString()));

        rdr.Close();
        req.GetResponse();

        // after uploading close stream 
        reqStream.Close();
    }
}





服务器代码





SERVER CODE

using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;

public partial class uploadfiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            LogInFile("First");
            HttpFileCollection uploadFile = Request.Files;
            LogInFile("Second");
            if (uploadFile.Count > 0)
            {
                HttpPostedFile postedFile = uploadFile[0];
                LogInFile("Thrid");
                System.IO.Stream inStream = postedFile.InputStream;
                LogInFile("Forth");
                byte[] fileData = new byte[postedFile.ContentLength];
                LogInFile("Fifth");
                inStream.Read(fileData, 0, postedFile.ContentLength);
                LogInFile("Sixth");
                postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName);
            }        
        }
        catch (Exception ex)
        { 
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Message : " +ex.Message);
            sb.AppendLine("Source : " + ex.Source);
            sb.AppendLine("StackTrace : " + ex.StackTrace);
            sb.AppendLine("InnerException : " + ex.InnerException);
            sb.AppendLine("ToString : " + ex.ToString());

            LogInFile(sb.ToString());
        }
    }
    public void LogInFile(string str)
    {
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = new StreamReader(Server.MapPath("Data") + "\\expfile.txt"))
        {            
            sb.AppendLine("= = = = = =");
            sb.Append(sr.ReadToEnd());
            sb.AppendLine();
            sb.AppendLine();
        }
        sb.AppendLine(str);
        using (StreamWriter outfile = new StreamWriter(Server.MapPath("Data") + "\\expfile.txt"))
        {
            outfile.Write(sb.ToString());
        }
    }
}


这篇关于如何在没有ftp的情况下将文件复制到c#中的远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆