求C#写windows应用程序实现上传附件功能源码! 如题,windows应用程序、C#语言实现上传附件功能,上传的附件保存到服务器上,并在数据库中记录下路径,望各位帮忙,万分感谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我做的是winform程序,不是web程序,用FtpWebRequest可行吗,我没有用过这个方法,VS.net2003有这个功能吗,我在msdn上怎么没查到? 可以用remoting或者webservices写一个服务器端的保存文件的方法,先后在客户端调用即可,这样做很简单。否则如果直接通过文件夹局域网访问就涉及到安全性和权限问题了。 我给你用FTP上传文件的方法:注意用FTP上传的时候先要在服务器上建立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; } } 要是winform ..用SOCKET 吧用着传输到服务器。 用FTP,或者WEBSERVICE吧,如果你不是很熟悉SOCKET最好不要用. 感谢8楼的朋友jhdxhj ,可我做的是winform程序,不是web程序,用FtpWebRequest可行吗,我没有用过这个方法,VS.net2003有这个功能吗,我在msdn上怎么没查到? 对呀。你的意思就是传输一些东西把。用socket处理完全可以。就是发送一些string或是2进制的东西。你在服务起那里处理一下。就可以了建议看看socket知识。你就明白了 FtpWebRequest 不分 winform 和 webform 都能用 我是8楼的,我做的winform就是这样传的,证明是没有问题的 webBrowser控件怎么只显示网页中的视频文件 vs2005重置工具箱devexpress组件在工具箱没有了 如何把PPT转换成图片 C#用传值 c# 怎样才能实现 像QQ那样的 自动的信息提示 框! 求助! 谢谢! vs2005 C#的注释下被插入一行或几行 web 页面传值时,如何把字符转为对象名? c# 设计模式的优缺点 如何在C#中调用VC++的DLL中返回类型为CString的函数? 請指點,那里有C#的數據庫系統的源代碼. 如何实现 向一个TEXT框输入内容,等输入完毕后自动判断其内容正确性的事件? 如何判断控件是什么类型的控件
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;
}
}
建议看看socket知识。你就明白了