windows 应用程序如何用C#实现上传附件功能? windows 应用程序如何用C#实现上传附件功能?请各位多多帮忙,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是把附件存到数据库还是服务器硬盘上?如果存到数据库中很简单,如果存到服务器硬盘上,你可以用remoting做一个服务器端程序 你也可以用FTP啊,服务器开个FTP的用户名,密码,再用C#写个上FTP的小程序,你看呢 看了半天,觉得LZ想要知道的是如何使用网络编程来上传文件,对吧??复杂的:你使用Socket,FTP方式上传到服务器,当然Socket需要你写个接收端,FTP,需要你的服务器开放FTP目录。简单的:System.IO.File.Copy(),这个方法可以本地拷贝文件,也同样可以远程拷贝文件。Copy()中的参数如果是远程路径,那么就是拷贝到远程路径上了。 FTP的 private void buttonDownload_Click(object sender, EventArgs e) { try { OpenFileDialog myfile = new OpenFileDialog(); if (myfile.ShowDialog() == DialogResult.OK) { foreach (Control control in this.Controls) { control.Update(); } } string path = myfile.FileName; sw.WriteLine("STOR " + path); sw.Flush(); sw.WriteLine("150"); sw.Flush(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); sw.WriteLine(fs.Length.ToString()); sw.Flush(); listBoxInfo.Items.Add("发送:" + fs.Length.ToString() + "字节"); for (int i = 0; i < fs.Length; i++) { netStream.WriteByte((byte)fs.ReadByte()); netStream.Flush(); this.progressBar1.Value = i; } fs.Close(); MessageBox.Show("上传完毕!"); this.progressBar1.Value = 0; } catch (System.Exception err) { MessageBox.Show(err.Message); } } 我给你一段用FTP方式上传的代码,你自己去漫漫的看: private bool FtpUpFile(string strFileName,string strFtpUrl,string strFtpUser,string strFtpPassword) { try { FileInfo fileInf = new FileInfo(strFileName); FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象 //reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(MainForm.m_sHotelSet + fileInf.Name)); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + fileInf.Name)); // ftp用户名和密码 //reqFTP.Credentials = new NetworkCredential(MainForm.m_sHotelSetFtpUser, MainForm.m_sHotelSetFtpPassword); reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPassword); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb contentLen = fs.Read(buff, 0, buffLength); int allbye = (int)fileInf.Length; int startbye = 0; // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入 upload stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); startbye += contentLen; } // 关闭两个流 strm.Close(); fs.Close(); return true; } catch { return false; } } iis建设中 C#运用程序从数据库SQl中取image格式的数据,到pictureBox控件显示 关于在wen.config关于修改权限的问题... [求助]求数据库连接字符串 C# 如何调用VB的组件? 谁有华为公司C#的面试题 Web页中动态创建控件,解决者,再给50分 想问一个C#操作Access的问题,有关查询的 C# 获取文件夹文件问题 .net 中类似java applet的功能如何实现? 激活弹出程序窗口 有关浅复制引用类型的问题
你使用Socket,FTP方式上传到服务器,当然Socket需要你写个接收端,FTP,需要你的服务器开放FTP目录。
简单的:
System.IO.File.Copy(),这个方法可以本地拷贝文件,也同样可以远程拷贝文件。Copy()中的参数如果是远程路径,那么就是拷贝到远程路径上了。
{
try
{
OpenFileDialog myfile = new OpenFileDialog();
if (myfile.ShowDialog() == DialogResult.OK)
{
foreach (Control control in this.Controls)
{
control.Update();
}
}
string path = myfile.FileName; sw.WriteLine("STOR " + path);
sw.Flush();
sw.WriteLine("150");
sw.Flush();
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
sw.WriteLine(fs.Length.ToString());
sw.Flush();
listBoxInfo.Items.Add("发送:" + fs.Length.ToString() + "字节");
for (int i = 0; i < fs.Length; i++)
{
netStream.WriteByte((byte)fs.ReadByte());
netStream.Flush();
this.progressBar1.Value = i;
}
fs.Close();
MessageBox.Show("上传完毕!");
this.progressBar1.Value = 0;
}
catch (System.Exception err)
{
MessageBox.Show(err.Message);
}
}
private bool FtpUpFile(string strFileName,string strFtpUrl,string strFtpUser,string strFtpPassword)
{
try
{
FileInfo fileInf = new FileInfo(strFileName);
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
//reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(MainForm.m_sHotelSet + fileInf.Name));
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + fileInf.Name));
// ftp用户名和密码
//reqFTP.Credentials = new NetworkCredential(MainForm.m_sHotelSetFtpUser, MainForm.m_sHotelSetFtpPassword);
reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
}
// 关闭两个流
strm.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}