请问WinForm如何上传文件

三木哥 2006-09-12 05:12:18
我用WinForm做的软件,是基于客户端-服务器的,我想在客户端将一个文件上传 到服务器,放在服务器的某个文件夹下,如何实现呢?
...全文
718 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanserzhao 2006-09-18
  • 打赏
  • 举报
回复
ASP.NET(C#)技术讨论群:30417196,限工作人士
路人霆 2006-09-18
  • 打赏
  • 举报
回复
WSE
sskset 2006-09-14
  • 打赏
  • 举报
回复
System.Net.WebClient

.UploadFileAsync
zw_angel 2006-09-14
  • 打赏
  • 举报
回复
可以用webservice
wshlxvb 2006-09-14
  • 打赏
  • 举报
回复
不是吧。VS里有自带的上传控件啊。
naturalth 2006-09-14
  • 打赏
  • 举报
回复
Dim strPathFrom, strPathTo, strGet As String
strPathFrom = "C:\Program Files\data"
strPathTo = "\\192.168.0.5\Public1\data"
' strGet = "D:\Public1\data"
Dim dir As System.IO.Directory
If Not System.IO.Directory.Exists(strPathFrom) Then
MessageBox.Show("文件不存在!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return
End If



'拷贝文件

For Each str As String In dir.GetFiles(strPathFrom)
System.IO.File.Copy(str, strPathTo)
Next
naturalth 2006-09-14
  • 打赏
  • 举报
回复
Dim dir As System.IO.Directory
这个dir是定义个文件类, strGet = "D:\Public1\data"文夹

ftp例子:
string FileName=@"d:\aln\teng.txt";
string UpLoadURL=@"ftp://app-server/temp";
string UserName=@"alan";
string Pwd=@"teng";
if( ifsuceful= Upload(FileName, UpLoadURL, UserName, Pwd))
{
//成功
}
Yorckzhou 2006-09-13
  • 打赏
  • 举报
回复
在你的服务器端有应用程序吗?
在服务器端建立一个文件上传的component,
发布为一个webservice,
然后在客户端调用这个webservice。即可以轻松实现。

在.Net 2005 studio上发布一个webservice非常方便。
yq_net 2006-09-13
  • 打赏
  • 举报
回复
xuexi
三木哥 2006-09-13
  • 打赏
  • 举报
回复
请问naturalth:
'拷贝文件
Dim strDBArray As String = ""
For Each str As String In dir.GetFiles(strPathFrom)
'copy files
System.IO.File.Copy(str, strPathTo)
Next
以上代码的dir是什么来的?还有strGet 变量是做什么用的?
通过ftp方式进行上传的方法,能不能写个调用该方法的例子呢?uploadUrl的格式该怎么写.
谢谢!

liangxf0022 2006-09-13
  • 打赏
  • 举报
回复
5楼正解
路人霆 2006-09-13
  • 打赏
  • 举报
回复
可以使用WSE.
naturalth 2006-09-13
  • 打赏
  • 举报
回复
如果你只是局域网的话那么你可以在局域网里面拷贝
你先可以通过先做个管道把使用"cmd.exe"连接下两台机器然后在进行文件拷贝
这个是之前我们用vb.net做的
'输入密码连接服务器
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("net use \\192.168.0.3 0328baopack /user:administrator")'注意这边通过net命令来建立连接,当然要确保有访问权限
p.StandardInput.WriteLine("exit")
p.Close()
System.Threading.Thread.CurrentThread.Sleep(4000)

Dim strPathFrom, strPathTo, strGet As String
'Dim result As DialogResult = FolderBrowserDialog1.ShowDialog()
'If (result = DialogResult.OK) Then
' strPath = FolderBrowserDialog1.SelectedPath
'Else
' Return
'End If
strPathFrom = "C:\Program Files\data"
strPathTo = "\\192.168.0.3\Public1\data"
strGet = "D:\Public1\data"

If Not System.IO.Directory.Exists(strPathFrom) Then
MessageBox.Show("文件不存在!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return
End If



'拷贝文件
Dim strDBArray As String = ""
For Each str As String In dir.GetFiles(strPathFrom)
'copy files
System.IO.File.Copy(str, strPathTo)
Next

还有可以通过ftp方式进行上传:
这个是通常用的个方法,这个我在做web程序时候使用的,在winform也测试通过.我建议你使用这个方法.当然用户名密码可以为空.这个取决与你的ftp的权限
private static bool Upload(string fileName, string uploadUrl, string UsrName, string PassWord)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
bool ifsucess = false;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(uploadUrl);

uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = new NetworkCredential(UsrName, PassWord);
// UploadFile is not supported through an Http proxy
// so we disable the proxy for this request.
uploadRequest.Proxy = null;

requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);

byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}

// The request stream must be closed before getting
// the response.
requestStream.Close();

uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
Console.WriteLine("Upload complete.");
ifsucess = true;
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
return ifsucess;
}
三木哥 2006-09-13
  • 打赏
  • 举报
回复
有没具体的实现啊
机器人 2006-09-12
  • 打赏
  • 举报
回复
用 NetworkStream
jhkII 2006-09-12
  • 打赏
  • 举报
回复
可以考慮通過FTP,如果文檔簡單,應該用System.IO操作就行了

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧