1. asp
asp 上传文件真的蛋疼,很麻烦,有时候就用第三方组件,或者比较复杂的写法来实现无组件上传。
测试OK的一个叫风声无组件上传类 V2.1 [Fonshen UpLoadClass Version 2.1] :风声 ASP 上传组件
用.net程序测试:
private void button1_Click(object sender, EventArgs e) { string url = "http://localhost/UploadFileTest/demo1/upload.asp"; WebClient wc = new WebClient(); //wc.Headers.Add("Content-Type", "multipart/form-data"); string file = "d:\33.mdb"; wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted); wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged); wc.UploadFileAsync(new Uri(url,UriKind.Absolute ), file ); } void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { Text = ""+e.ProgressPercentage; } void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { MessageBox.Show(Encoding.UTF8.GetString(e.Result)); MessageBox.Show("upload ok"); }
2. ASP.NET
服务端:
using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Files.Count > 0) { Request.Files[0].SaveAs(Server.MapPath("~/") + Path.GetFileName(Request.Files[0].FileName)); Label1.Text = "上传成功!"; } else { Label1.Text = "no file!"; } } }
客户端可以用上面那个 用.net程序测试。
改下 url = "http://localhost:4567/WebSite2/Default.aspx";//假如这个是服务端保存上传文件的东西。
3.VB6 上传文件
VB6 不好实现,可借用第三方组件:ChilkatHttp
Private Sub Command1_Click() Dim url As String url = "http://localhost:4567/WebSite2/Default.aspx" ' This example requires the Chilkat API to have been previously unlocked. ' See Global Unlock Sample for sample code. Dim TlsUnlockCode As String TlsUnlockCode = "xxxxxxxxxxxxxxxx" '正式版解锁代码,需要购买 Dim http As New ChilkatHttp unLockSuccess = http.UnlockComponent(TlsUnlockCode) 'or call UnlockComponetChilkatHttp(oHttpServer) Dim req As New ChilkatHttpRequest req.HttpVerb = "POST" req.contentType = "multipart/form-data" req.Path = "WebSite2/Default.aspx" ' Send an "Expect: 100-continue" header in the request. ' This causes the HTTP server to end a 100-continue response ' immediately after receiving the HTTP header. The client ' (Chilkat) will receive this intermediate response, and if ' it's not an error response, it knows that the HTTP server will ' accept the data that is forthcoming. ' The alternative is to get an error response after trying to upload ' the entire contents of the files. req.AddHeader "Expect", "100-continue" ' Call AddFileForUpload2 for each file to be uploaded in the HTTP multipart/form-data POST ' To allow Chilkat to determine the content-type automatically based on file-extension, ' call AddFileForUpload instead. ' The 1st arg is the filename passed in the HTTP request. ' The 2nd arg is the path in the local filesytem. ' The file is not loaded into memory. It is streamed directly from the file ' when the HTTP POST is sent. Dim success As Long success = req.AddFileForUpload("33.mdb", "d:/33.mdb") 'req.AddFileForUpload2("1.jpg", "d:/1.jpg", "image/jpg") If (success <> 1) Then MsgBox req.LastErrorText Exit Sub End If ' http://www.mywebserver123abc.com/rcvFormDataUpload.aspx Dim resp As ChilkatHttpResponse Set resp = http.SynchronousRequest("localhost", 4567, 0, req) If (http.LastMethodSuccess <> 1) Then Debug.Print http.LastErrorText Else Debug.Print "HTTP response status: " & resp.StatusCode ' See the online reference documentation for ' other information that can be obtained from the response object. End If Exit Sub '------------------------------------------------------------------------ ' To send using SSL/TLS, do this instead. ' This sends to https://www.mywebserver123abc.com/rcvFormDataUpload.aspx Set resp = http.SynchronousRequest("www.mywebserver123abc.com", 443, 1, req) If (http.LastMethodSuccess <> 1) Then Debug.Print http.LastErrorText Else Debug.Print "HTTP response status: " & resp.StatusCode ' See the online reference documentation for ' other information that can be obtained from the response object. End If End Sub