ASP/ASP.NET/VB6文件上傳

1. asphtml

asp 上傳文件真的蛋疼,很麻煩,有時候就用第三方組件,或者比較複雜的寫法來實現無組件上傳。web

測試OK的一個叫風聲無組件上傳類 V2.1 [Fonshen UpLoadClass Version 2.1] :風聲 ASP 上傳組件測試

用.net程序測試:ui

 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.NETthis

服務端:url

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程序測試。spa

改下   url = "http://localhost:4567/WebSite2/Default.aspx";//假如這個是服務端保存上傳文件的東西。.net

 

3.VB6 上傳文件code

VB6 很差實現,可借用第三方組件:ChilkatHttporm

 

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
相關文章
相關標籤/搜索