读书人

求C# windows应用程序上传文件到指定I

发布时间: 2012-04-21 14:34:44 作者: rapoo

求C# windows应用程序上传文件到指定IP的机器上。
例如:

窗体1 : 有个按钮,图片框,当点这个按钮的时候 就传送1.jpg 到IP为 192.168.1.2的机器的D:\images下。

传送成功后,图片框从192.168.1.2 的机器上 加载D:\images\1.jpg

怎么实现这个过程。

说白了 就是 实现类似QQ头像上传的效果的。上传到服务器。并且下次登陆从服务器那边取出来。

最好有关键部分的代码。

[解决办法]
首先必须上传的服务器支持文件共享或者Web共享,当然可以通过Web Service之类的方法来解决。
上传时将文件数据和文件名作为参数传过去,在目标服务器上保存这些数据;
获取的时候,将文件名作为参数穿过去,目标服务器将文件数据传过来。
[解决办法]
通过使用FTPwebrequest上传文件
或web services
private bool FtpUpFile(string strFileName,string strFtpUrl,string strFtpUser,string strFtpPassword)
{
try
{
FileInfo fileInf = new FileInfo(strFileName);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
}
strm.Close();
fs.Close();
return true;
}
catch
{
return false;
}


}
[解决办法]

C# code
 
/// <summary>
/// 建立DOS连接
/// </summary>
/// <param name="path">要建立连接的服务器IP以及路径 </param>
static public void DOSConnected()
{
string path = GetServerSavePath();
Process proc = new Process(); //实例化一个Process类
proc.StartInfo.FileName = "cmd.exe"; //设定程序名
proc.StartInfo.Arguments = "/c net use Z: \\\\" + path; //设定程式执行参数,此命令意思是将参数path代表的服务器路径虚拟为本地的Z盘
proc.StartInfo.CreateNoWindow = false; //设置不显示窗口
proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
proc.StartInfo.RedirectStandardError = true; //重定向错误输出
proc.Start(); //启动
proc.WaitForExit();
}
/// <summary>
/// 文件拷贝 从网站服务器拷贝到文件服务器
/// </summary>
/// <param name="FilePath">本地文件路径 </param>
static public void DOSXCopy(string FilePath)
{
try
{
Process proc = new Process(); //实例化一个Process类


proc.StartInfo.FileName = "cmd.exe"; //设定程序名
proc.StartInfo.Arguments = "/c XCOPY " + FilePath + " Z:\\";//此命令意思是使用XCOPY命令将FilePath表示的文件拷贝到Z盘
proc.StartInfo.CreateNoWindow = false; //设置不显示窗口
proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
proc.StartInfo.RedirectStandardError = true; //重定向错误输出
proc.Start();
proc.WaitForExit();
}
catch(Exception ex)
{
string time = System.DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分", DateTimeFormatInfo.InvariantInfo);
string sss = "图片保存日志.txt";
//指定路径
sss = "d:\\" + sss;
//如果文件a.txt存在就打开,不存在就新建 .append 是追加写
FileStream fst = new FileStream(sss, FileMode.Append);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + '\r' + "保存路径为:" + '\r' + FilePath + '\r' + "出错为:" + '\r' + ex);
swt.Close();
fst.Close();
}
}
/// <summary>
/// 文件拷贝 从文件服务器到网站服务器
/// </summary>
/// <param name="FilePath">本地文件路径 </param>
static public void DOSDownLode(string FileName, string LocalPath)
{
Process proc = new Process(); //实例化一个Process类
proc.StartInfo.FileName = "cmd.exe"; //设定程序名
proc.StartInfo.Arguments = "/c XCOPY Z:\\" + FileName + " " + LocalPath;//此命令意思是使用XCOPY命令将FilePath表示的文件拷贝到Z盘
proc.StartInfo.CreateNoWindow = false; //设置不显示窗口
proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
proc.StartInfo.RedirectStandardError = true; //重定向错误输出
proc.Start();
proc.WaitForExit();
}
/// <summary>
/// 删除与服务器的连接
/// </summary>
static public void DOSDelConnected()
{
Process proc = new Process(); //实例化一个Process类
proc.StartInfo.FileName = "cmd.exe"; //设定程序名
proc.StartInfo.Arguments = "/c net use Z: /delete ";//此命令意思是删除与虚拟盘Z的连接
proc.StartInfo.CreateNoWindow = false; //设置不显示窗口
proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
proc.StartInfo.RedirectStandardError = true; //重定向错误输出
proc.Start();
proc.Kill();
proc.WaitForExit();
proc.Refresh();
}

/// <summary>
/// 删除文件服务器的文件
/// </summary>
static public void DOSDelFile(string FileName)
{
Process proc = new Process(); //实例化一个Process类
proc.StartInfo.FileName = "cmd.exe"; //设定程序名
proc.StartInfo.Arguments = "/c del Z:\\" + FileName;//此命令意思是删除Z盘下名为FileName的文件


proc.StartInfo.CreateNoWindow = false; //设置不显示窗口
proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
proc.StartInfo.RedirectStandardError = true; //重定向错误输出
proc.Start();
proc.WaitForExit();
}

读书人网 >C#

热点推荐