Visual Basic C++ J# JScript //--------------------------------------------------------------------- // This file is part of the Microsoft .NET Framework SDK Code Samples. // // Copyright (C) Microsoft Corporation. All rights reserved. // //This source code is intended only as a supplement to Microsoft //Development Tools and/or on-line documentation. See these other //materials for detailed information regarding Microsoft code samples. // //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A //PARTICULAR PURPOSE. //---------------------------------------------------------------------#region Using directivesusing System; using System.Text; using System.IO; using System.Net; #endregionnamespace Microsoft.Samples.FtpSample { static class Program { static void Main(string[] args) { if (args.Length == 0 || args[0].Equals("/?")) { DisplayUsage(); } else if (args.Length == 1) { Download(args[0]); } else if (args.Length == 2) { if (args[0].Equals("/list")) List(args[1]); else Upload(args[0], args[1]); } else { Console.WriteLine("Unrecognized argument."); } } static void DisplayUsage() { Console.WriteLine("USAGE:"); Console.WriteLine(" FtpSample [/? | <FTP download URL> | <local file>"); Console.WriteLine(" <FTP upload URL> | /list <FTP list URL>]"); Console.WriteLine(); Console.WriteLine("where"); Console.WriteLine(" FTP download URL URL of a file to download from an FTP server."); Console.WriteLine(" FTP upload URL Location on a FTP server to upload a file to."); Console.WriteLine(" FTP list URL Location on a FTP server to list the contents of."); Console.WriteLine(" local file A local file to upload to an FTP server."); Console.WriteLine(); Console.WriteLine(" Options:"); Console.WriteLine(" /? Display this help message."); Console.WriteLine(" /list Specifies the list command."); Console.WriteLine(); Console.WriteLine("EXAMPLES:"); Console.WriteLine(" Download a file FtpSample ftp://myserver/download.txt"); Console.WriteLine(" Upload a file FtpSample upload.txt ftp://myserver/upload.txt"); } static void Download(string downloadUrl) { Stream responseStream = null; FileStream fileStream = null; StreamReader reader = null; try { FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl); FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse(); responseStream = downloadResponse.GetResponseStream(); string fileName = Path.GetFileName(downloadRequest.RequestUri.AbsolutePath); if (fileName.Length == 0) { reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); } else { fileStream = File.Create(fileName); byte[] buffer = new byte[1024]; int bytesRead; while (true) { bytesRead = responseStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; fileStream.Write(buffer, 0, bytesRead); } } Console.WriteLine("Download complete."); } catch (UriFormatException ex) { Console.WriteLine(ex.Message); } catch (WebException ex) { Console.WriteLine(ex.Message); } catch (IOException ex) { Console.WriteLine(ex.Message); } finally { if (reader != null) reader.Close(); else if (responseStream != null) responseStream.Close(); if (fileStream != null) fileStream.Close(); } } static void Upload(string fileName, string uploadUrl) { Stream requestStream = null; FileStream fileStream = null; FtpWebResponse uploadResponse = null; try { FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uploadUrl); uploadRequest.Method = WebRequestMethods.Ftp.UploadFile; // UploadFile is not supported through an Http proxy // so we disable the proxy for this request. uploadRequest.Proxy = null; requestStream = uploadRequest.GetRequestStream(); fileStream = File.Open(fileName, FileMode.Open); byte[] buffer = new byte[1024]; int bytesRead; while (true) { bytesRead = fileStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; requestStream.Write(buffer, 0, bytesRead); }
Source: FtpSample.cs
Visual Basic C++
J#
JScript
//---------------------------------------------------------------------
// This file is part of the Microsoft .NET Framework SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------#region Using directivesusing System;
using System.Text;
using System.IO;
using System.Net;
#endregionnamespace Microsoft.Samples.FtpSample
{
static class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || args[0].Equals("/?"))
{
DisplayUsage();
}
else if (args.Length == 1)
{
Download(args[0]);
}
else if (args.Length == 2)
{
if (args[0].Equals("/list"))
List(args[1]);
else
Upload(args[0], args[1]);
}
else
{
Console.WriteLine("Unrecognized argument.");
}
}
static void DisplayUsage()
{
Console.WriteLine("USAGE:");
Console.WriteLine(" FtpSample [/? | <FTP download URL> | <local file>");
Console.WriteLine(" <FTP upload URL> | /list <FTP list URL>]");
Console.WriteLine();
Console.WriteLine("where");
Console.WriteLine(" FTP download URL URL of a file to download from an FTP server.");
Console.WriteLine(" FTP upload URL Location on a FTP server to upload a file to.");
Console.WriteLine(" FTP list URL Location on a FTP server to list the contents of.");
Console.WriteLine(" local file A local file to upload to an FTP server.");
Console.WriteLine();
Console.WriteLine(" Options:");
Console.WriteLine(" /? Display this help message.");
Console.WriteLine(" /list Specifies the list command.");
Console.WriteLine();
Console.WriteLine("EXAMPLES:");
Console.WriteLine(" Download a file FtpSample ftp://myserver/download.txt");
Console.WriteLine(" Upload a file FtpSample upload.txt ftp://myserver/upload.txt");
} static void Download(string downloadUrl)
{
Stream responseStream = null;
FileStream fileStream = null;
StreamReader reader = null;
try
{
FtpWebRequest downloadRequest =
(FtpWebRequest)WebRequest.Create(downloadUrl);
FtpWebResponse downloadResponse =
(FtpWebResponse)downloadRequest.GetResponse();
responseStream = downloadResponse.GetResponseStream(); string fileName =
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath); if (fileName.Length == 0)
{
reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
}
else
{
fileStream = File.Create(fileName);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
}
Console.WriteLine("Download complete.");
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (reader != null)
reader.Close();
else if (responseStream != null)
responseStream.Close();
if (fileStream != null)
fileStream.Close();
}
} static void Upload(string fileName, string uploadUrl)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(uploadUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile; // UploadFile is not supported through an Http proxy
// so we disable the proxy for this request.
uploadRequest.Proxy = null; requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open); byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
// The request stream must be closed before getting
// the response.
requestStream.Close(); uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
Console.WriteLine("Upload complete.");
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
} private static void List(string listUrl)
{
StreamReader reader = null;
try
{
FtpWebRequest listRequest =
(FtpWebRequest)WebRequest.Create(listUrl);
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse listResponse =
(FtpWebResponse)listRequest.GetResponse();
reader = new StreamReader(listResponse.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("List complete.");
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (reader != null)
reader.Close();
}
}
}
}
<%@ Page language="c#" Codebehind="upfiles.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>多文件上传</title>
<script language="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<P id="MyFile"><INPUT type="file" size="50" NAME="File"></P>
<P>
<input type="button" value="增加(Add)" onclick="addFile()"> <input onclick="this.form.reset()" type="button" value="重置(ReSet)">
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</P>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" Width="500px"
BorderStyle="None" BorderColor="White"></asp:Label>
</P>
</div>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebPortal
{
/// <summary>
/// UpLoad 的摘要说明。
/// 实现多文件上传
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus; private void Page_Load(object sender, System.EventArgs e)
{
/// 在此处放置用户代码以初始化页面
if (this.IsPostBack) this.SaveImages();
} private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images /") + fileName);
//postedFile.SaveAs("E:"+"\\"+temp+"\\"+fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}但是如何在网面上显示下载的连接,还没有解决!怎么为啊????