winform 上传图片 要求1.将图片上传到指定的文件夹下2.给图片重新命名,截取图片中的图片名存到数据库3.代码尽量详细点有注释最好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 winform 不叫上传 叫复制(反正都在本地)把原先的图片复制到相应的路径下 那就要用到TCP UDP 等协议传文件了 使用BinaryStreamFtp上传的话,网上有现成的封装好的类可使用,搜一下吧 openfiledialogfile.move上传使用webclient ///<summary> /// WebClient上传文件至服务器 ///</summary> ///<param name="localFilePath">文件名,全路径格式</param> ///<param name="serverFolder">服务器文件夹路径</param> ///<param name="reName">是否需要修改文件名,这里默认是日期格式</param> ///<returns></returns> public static bool UploadFile(string localFilePath, string serverFolder,bool reName) { string fileNameExt, newFileName, uriString; if (reName) { fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") +1); newFileName = DateTime.Now.ToString("yyMMddhhmmss.") + fileNameExt; } else { newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\")+1); } if (!serverFolder.EndsWith("/") &&!serverFolder.EndsWith("\\")) { serverFolder = serverFolder +"/"; } uriString = serverFolder + newFileName; //服务器保存路径 /**//**//**//// 创建WebClient实例 WebClient myWebClient =new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 FileStream fs = new FileStream(localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1), FileMode.Open, FileAccess.Read); BinaryReader r =new BinaryReader(fs); try { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString,"PUT",localFilePath); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); } else { MessageBox.Show("文件目前不可写!"); } postStream.Close(); } catch { //MessageBox.Show("文件上传失败,请稍候重试~"); return false; } return true; }唉,为了分啊 如何获得服务器路径呢?比如我想把图片上传到 ip 为192.168.0.3的D盘Images文件下?????????? /// <summary> /// 获取服务器IP /// </summary> /// <returns></returns> public static string GetIp() { string Ip = ""; string strConn = DBUtil.SqlConnectionManager.ConnectionString(); int startIndex = strConn.IndexOf("=") + 1; int endIndex = strConn.IndexOf(";"); int totalLength = strConn.Length; int length = totalLength - startIndex - (totalLength - endIndex); Ip = strConn.Substring(startIndex, length); return Ip; } try { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString,"PUT",localFilePath); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); } else { MessageBox.Show("文件目前不可写!"); } postStream.Close(); 能不能给上面的代码加下注释啊?还有就是我Catch出来的错误是“无法连接到远程服务器”谢谢各位的关注了,本人确实分不多,希望大家可以谅解。能帮我把问题解决,O(∩_∩)O谢谢 http://www.cnblogs.com/vaiyanzi/archive/2008/11/20/1337465.html 您可以通过如下的方法实现从win application中upload file假设上传目录的物理路径为c:\upload,url为http://localhost/upload1.在IIS中upload虚拟目录属性中的directory security中的anonymous access and authentication control一栏中,点击edit,选中Anonymous access,并在virtual directory一栏选中write属性。2.将c:\upload目录属性中的Security设置为everyone3.在程序中使用如下的代码就可以实现file upload WebClient myclient = new WebClient(); myclient.UploadFile ("http://localhost/upload/odbc.ini","PUT","e:\\temp\\ODBC.INI");——微软全球技术中心 技术支持我只能做这么多了 为什么tabStrip在切换时子窗口不最大化了? 再谈加密app.config,且在任何电脑都能用. 在WebForm中如何动态修改IE打印“页面设置”中的参数??? 怪事了,写WINDOWS程序不能引入System.Web.Mail命名空间?那怎么发邮件呢!? 如何让编辑框中的光标跑到最右边啊? 怎样将base16编码的字符串转化为base64编码的字符串? 多线程,解决者100分相送...... 想学c#,请大虾们推荐几本好书 属性的问题 超级菜鸟送分,一小时后给分。 新手问题:函数中使用了foreach还没运行完直接return有没有坏处? 有滚动条的时候保存图片的问题
那就要用到TCP UDP 等协议传文件了
Ftp上传的话,网上有现成的封装好的类可使用,搜一下吧
file.move
上传使用webclient
///<summary>
/// WebClient上传文件至服务器
///</summary>
///<param name="localFilePath">文件名,全路径格式</param>
///<param name="serverFolder">服务器文件夹路径</param>
///<param name="reName">是否需要修改文件名,这里默认是日期格式</param>
///<returns></returns>
public static bool UploadFile(string localFilePath, string serverFolder,bool reName)
{
string fileNameExt, newFileName, uriString;
if (reName)
{
fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") +1);
newFileName = DateTime.Now.ToString("yyMMddhhmmss.") + fileNameExt;
}
else
{
newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\")+1);
} if (!serverFolder.EndsWith("/") &&!serverFolder.EndsWith("\\"))
{
serverFolder = serverFolder +"/";
} uriString = serverFolder + newFileName; //服务器保存路径
/**//**//**//// 创建WebClient实例
WebClient myWebClient =new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件
FileStream fs = new FileStream(localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1), FileMode.Open, FileAccess.Read);
BinaryReader r =new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
{
//MessageBox.Show("文件上传失败,请稍候重试~");
return false;
} return true;
}唉,为了分啊
/// <summary>
/// 获取服务器IP
/// </summary>
/// <returns></returns>
public static string GetIp()
{
string Ip = "";
string strConn = DBUtil.SqlConnectionManager.ConnectionString();
int startIndex = strConn.IndexOf("=") + 1;
int endIndex = strConn.IndexOf(";");
int totalLength = strConn.Length;
int length = totalLength - startIndex - (totalLength - endIndex);
Ip = strConn.Substring(startIndex, length);
return Ip;
}
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
能不能给上面的代码加下注释啊?还有就是我Catch出来的错误是“无法连接到远程服务器”谢谢各位的关注了,本人确实分不多,希望大家可以谅解。能帮我把问题解决,O(∩_∩)O谢谢