怎么样在visual c#中实现文件的上传??????紧急求救高手指教

badaoqi_bby 2005-12-29 05:39:01
我想在visual c#中实现文件的上传和下载请各位高手指教
...全文
180 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cenlmmx 2005-12-30
  • 打赏
  • 举报
回复
那就用socket通讯,ftp,http等都是建立在它上面的.
private void ftp()
{
string result = http("ip地址", 80);
MessageBox.Show(result);
}

private string http(string server, int port)
{
//Set up variables and String to write to the server.
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: " + server+
"\r\nConnection: Close\r\n\r\n";
//string Get = "ls \r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;

// Create a socket connection with the specified server and port.
Socket s = connectSocket(server, port);

if (s == null)
return ("Connection failed");

// Send request to the server.
s.Send(ByteGet, ByteGet.Length, 0);


// Receive the server home page content.
Int32 bytes = s.Receive(RecvBytes,
RecvBytes.Length, 0);

// Read the first 256 bytes.
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage +
ASCII.GetString(RecvBytes, 0, bytes);


while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}

return strRetPage;
}

private static Socket connectSocket(string server, int port)
{
Socket s = null;
IPHostEntry iphe = null;


try
{
// Get host related information.
iphe = Dns.Resolve(server);


// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception to be thrown if the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress ipad in iphe.AddressList)
{
IPEndPoint ipe = new IPEndPoint(ipad, port);

Socket tmpS = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

tmpS.Connect(ipe);

if(tmpS.Connected)
{
s = tmpS;
break;
}
else
continue;
}
}

catch(SocketException)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception)
{
}
return s;

}
guopeng_28 2005-12-30
  • 打赏
  • 举报
回复
那你跑错地方了,C#谁也没规定要写asp.net,写form也是一样的,
实现的方法是一样的,用文件流的方式进行读写,
像图片这样的东西,就用二进制流读写就行了,
去MSDN找一下关于流的操作就容易了
badaoqi_bby 2005-12-30
  • 打赏
  • 举报
回复
上面的各位说的都是asp.net中的应用,我想问的是visual c#中的,就象delphi中实现文件上传的功能.谢谢各位!
zeusvenus 2005-12-30
  • 打赏
  • 举报
回复
关于文件大小限制问题可以在IIS设置中解决.
以上都说得是webform的,如果是windows应用程序,我这也有简单示例.
zeusvenus 2005-12-30
  • 打赏
  • 举报
回复
去博客园Bestcomy的blog上看看或搜索bestcomy aspnetupload
有现成的.如果找不到可以发消息给我告诉邮箱我发给你代码.
zhouabc 2005-12-29
  • 打赏
  • 举报
回复


好象ASP.NET上传文件最大只能4M?
califord 2005-12-29
  • 打赏
  • 举报
回复
呵呵,楼上的各位说的不错,我记下了
yumenkaifa 2005-12-29
  • 打赏
  • 举报
回复
上传图片到本地硬盘
if(WebFile.PostedFile.FileName=="")
{
Info.Text="请先选择要上传的文件";
return;
}


char[] spliter = {'\\'};
string [] FileName = WebFile.PostedFile.FileName.Split(spliter,10);
string FullPath = CurrentPath + @"\" + FileName[FileName.Length-1]; //生成完整文件名
WebFile.PostedFile.SaveAs(FullPath); //保存文件
Info.Text="上传文件成功,请与管理员联系";
c11_11_11 2005-12-29
  • 打赏
  • 举报
回复
//上传图片的程序段
DateTime now = DateTime.Now ;
//取现在时间到DataTime类的对象now中
string strBaseLocation = "D:\\web\\FC\\pic\\";
//这是文件将上传到的服务器的绝对目录
if (uploadfile1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
{
uploadfile1.PostedFile.SaveAs(strBaseLocation+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg");
//执行上传,并自动根据日期和文件大小不同为文件命名,确保不重复
Label1.Text="图片1已经上传,文件名为:"+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg";
 navigator.Insert(System.Xml.TreePosition.After, XmlNodeType.Element,"pic1","","") ;
navigator.Insert(System.Xml.TreePosition.FirstChild, XmlNodeType.Text,"pic1","","") ;
navigator.Value= now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg" ;
navigator.MoveToParent() ;
}
ChengKing 2005-12-29
  • 打赏
  • 举报
回复
(一)上传
1.
<INPUT id="WebFile" style="WIDTH: 490px; HEIGHT: 22px" type="file" size="62" name="WebFile" runat="server">
protected System.Web.UI.HtmlControls.HtmlInputFile WebFile;
文件上传参考代码:
/// <summary>
/// 文件上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnUpload_Click(object sender, System.EventArgs e)
{
if(WebFile.PostedFile.FileName=="")
{
Info.Text="请先选择要上传的文件";
return;
}

try
{
char[] spliter = {'\\'};
string [] FileName = WebFile.PostedFile.FileName.Split(spliter,10);

string FullPath = CurrentPath + @"\" + FileName[FileName.Length-1]; //生成完整文件名
WebFile.PostedFile.SaveAs(FullPath); //保存文件
LoadDir(CurrentPath); //重新载入当前目录
}
catch
{
Info.Text="上传文件失败,请与管理员联系";
}
}

2.
http://www.gdcic.net/dotnetBank/ViewContent.aspx?artid=000000000186


(二)下载

1. C#:
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}


2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))

'繁体格式
'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))

'向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream"
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub

110,499

社区成员

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

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

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