C#文件上传 急求:C# 基于WIN窗体的:如何将本地文件上传到服务器 ,望给出源代码,谢谢! 如将本地磁盘d盘的文件 2008.doc 上传到服务器192.168.10.2上的文件“上传文件”中,如何实现 望高手指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用IIS建了一个FTP FTP可以通过地址访问,但怎样通过代码来访问FTP呢谢谢 能帮我写个么 using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.OracleClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.IO; namespace test{/// <summary>/// postexercise 的摘要说明。/// </summary>public class postexercise : System.Web.UI.Page{ protected System.Web.UI.WebControls.Button btnsubmit1; protected System.Web.UI.WebControls.Label lblMsg1; protected System.Web.UI.WebControls.Label lblMsg2; protected System.Web.UI.WebControls.Button btnsubmit; protected System.Web.UI.HtmlControls.HtmlInputFile file1; protected System.Web.UI.HtmlControls.HtmlInputFile File2; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 }private void btnsubmit_Click(object sender, System.EventArgs e) { int res=0; if(res==-999) { lblMsg1.Text="文件上传失败1 !"; return; } //获得文件名 string filename=System.IO.Path.GetFileName(file1.PostedFile.FileName); filename=Server.MapPath("../test/Excel/")+filename; //保存上传文件 res=SaveExcelFile(filename); if(res==1) { lblMsg1.Text="文件上传成功 !"; } else { lblMsg1.Text="文件上传失败2 !"; return; } } private int SaveExcelFile(string filename) { try { file1.PostedFile.SaveAs(filename); return 1; } catch { return -999; } }}} private void Upload(string filename){FileInfo fileInf = new FileInfo(filename);string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;FtpWebRequest reqFTP;// 根据uri创建FtpWebRequest对象reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));// ftp用户名和密码reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// 默认为true,连接不会被关闭// 在一个命令之后被执行reqFTP.KeepAlive = false;// 指定执行什么命令reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定数据传输类型reqFTP.UseBinary = true;// 上传文件时通知服务器文件的大小reqFTP.ContentLength = fileInf.Length;// 缓冲大小设置为2kbint buffLength = 2048;byte[] buff = new byte[buffLength];int contentLen;// 打开一个文件流 (System.IO.FileStream) 去读上传的文件FileStream fs = fileInf.OpenRead();try{// 把上传的文件写入流Stream strm = reqFTP.GetRequestStream();// 每次读文件流的2kbcontentLen = fs.Read(buff, 0, buffLength);// 流内容没有结束while (contentLen != 0){// 把内容从file stream 写入 upload streamstrm.Write(buff, 0, contentLen);contentLen = fs.Read(buff, 0, buffLength);}// 关闭两个流strm.Close();fs.Close();}catch (Exception ex){MessageBox.Show(ex.Message, "Upload Error");}}以上是采用.Net所带的FtpWebRequest类实现的,楼主也可以用完全自己完成FTp协议的方法实现,可以参考以下链接:http://www.csharphelp.com/archives/archive9.html private void Upload(string filename) 中的filename 应该是本地文件的目录吧》?string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 中的fileInf.Name 就是只本地文件filename文件的名字么?想问你的是:本地文件的目录到底付给哪个参数? 求助:如何获取控制台程序的输出文本 如何实现DATAGRIDVIEW的多维表头的问题。.net控件达人进来看一看吧 问个关于托拽的问题 如何用C#语言实现以下递归方法? 进度条异步显示问题?高分请高手回答 有关C#内存问题 C #如何判断打印机缺纸? 请教catch,finally的几个问题 datagrid中的按钮列中的编辑列的控制问题? 关于MdiList的问题,最好有微软的朋友看一下 UDP监控的问题 子窗口控制主窗口里控件属性
谢谢 能帮我写个么
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace test
{
/// <summary>
/// postexercise 的摘要说明。
/// </summary>
public class postexercise : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnsubmit1;
protected System.Web.UI.WebControls.Label lblMsg1;
protected System.Web.UI.WebControls.Label lblMsg2;
protected System.Web.UI.WebControls.Button btnsubmit;
protected System.Web.UI.HtmlControls.HtmlInputFile file1;
protected System.Web.UI.HtmlControls.HtmlInputFile File2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private void btnsubmit_Click(object sender, System.EventArgs e)
{
int res=0;
if(res==-999)
{
lblMsg1.Text="文件上传失败1 !";
return;
}
//获得文件名
string filename=System.IO.Path.GetFileName(file1.PostedFile.FileName);
filename=Server.MapPath("../test/Excel/")+filename;
//保存上传文件
res=SaveExcelFile(filename);
if(res==1)
{
lblMsg1.Text="文件上传成功 !";
}
else
{
lblMsg1.Text="文件上传失败2 !";
return;
}
}
private int SaveExcelFile(string filename)
{
try
{
file1.PostedFile.SaveAs(filename);
return 1;
}
catch
{
return -999;
}
}}
}
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为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();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}以上是采用.Net所带的FtpWebRequest类实现的,
楼主也可以用完全自己完成FTp协议的方法实现,可以参考以下链接:
http://www.csharphelp.com/archives/archive9.html
private void Upload(string filename) 中的filename 应该是本地文件的目录吧》?
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 中的fileInf.Name 就是只本地文件filename文件的名字么?想问你的是:本地文件的目录到底付给哪个参数?