類比file的上傳的幾種替代方法

來源:互聯網
上載者:User

在WEB常常有人問我如何直接給file發檔案呢??

通常都告訴別人可以使wscript.shell用sendkeys類比,但是那個是沒有用處的,如果僅僅是不喜歡file控制項呢,可以用層把file透明了,用button結合text,兩種方法具體如下:

這個是方法2:
<div style="position:absolute;top:11px;left:-71px;width:150;filter:alpha(opacity=0)" >
  <input name="file" type="file" size=33 onpropertychange="a.value=this.value">
</div>

方法一是:
<script  language=javascript>  
function  window.onload()  
{  
       document.form1.T1.focus();  
       var  WshShell=new  ActiveXObject("WScript.Shell");  
       WshShell.sendKeys("c://test.txt");  //向文字框裡發送東東  
}  
</script>  

這並不是所有的,我以前還寫過一個VB的ftp上傳,做成控制項定時傳送的

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal LAccessType As Long, ByVal sProxyName As String, _
ByVal SProxyBypass As String, ByVal lFlags As Long) As Long

Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUsername As String, _
ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long

Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean

Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean

Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" _
  (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
 
Public Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" _
  (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean
 
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

Private Sub Timer1_Timer()
lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _
   vbNullString, vbNullString, 0&)
If lnginet Then
  lnginetconn = InternetConnect(lnginet, "210.51.180.101", 0, _
        "使用者名稱", "密碼", 1, 0, 0)
  If lnginetconn Then
      blnrc = FtpPutFile(lnginetconn, "你的具體上傳的東東", "網站的FTP的檔案夾", 0, 0, 1, 0)
      If blnrc Then
        MsgBox "download ok!!!"
      End If
      InternetCloseHandle lnginetconn
      InternetCloseHandle lnginet
    Else
    a = 1
  DoEvents
    End If
  Else
     a = 2
     DoEvents
    
  End If
End Sub
當然這個也不最後的,因為別人畢竟要得是http的上傳

研究了一下發現一個好辦法是用httpsendrequest函數,可以用http協議直接上傳,唯一的問題是用戶端要允許你註冊
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize)
{
        INTERNET_BUFFERS BufferIn;
        DWORD dwBytesWritten;
        int n;
        BYTE pBuffer[1024];
        BOOL bRet;

        BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
// Must be set or error will occur
    BufferIn.Next = NULL;
    BufferIn.lpcszHeader = NULL;
    BufferIn.dwHeadersLength = 0;
    BufferIn.dwHeadersTotal = 0;
    BufferIn.lpvBuffer = NULL;               
    BufferIn.dwBufferLength = 0;
    BufferIn.dwBufferTotal = dwPostSize;
// This is the only member used other than dwStructSize
    BufferIn.dwOffsetLow = 0;
    BufferIn.dwOffsetHigh = 0;

    if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))
    {
        printf( "Error on HttpSendRequestEx %d/n",GetLastError() );
        return FALSE;
    }

        FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data

        bRet=TRUE;
        for(n=1; n<=(int)dwPostSize/1024 && bRet; n++)
        {
     if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten))
                        printf( "/r%d bytes sent.", n*1024);
        }
               
        if(!bRet)
        {
        printf( "/nError on InternetWriteFile %lu/n",GetLastError() );
        return FALSE;
    }

    if(!HttpEndRequest(hRequest, NULL, 0, 0))
    {
        printf( "Error on HttpEndRequest %lu /n", GetLastError());
        return FALSE;
    }

        return TRUE;
}
這個是用VC寫的,道理是相同的,可以在MSDN上查到httpsendrequest的原型,上面那個程式的痛點就是把檔案分割了,以便於ASP的接收

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.