1. 程式人生 > >使用CURL上傳檔案curl_formadd的一個例子

使用CURL上傳檔案curl_formadd的一個例子

 /*****************************************************************************
 *                                  _   _ ____  _    
 *  Project                     ___| | | |  _ /| |   
 *                             / __| | | | |_) | |   
 *                            | (__| |_| |  _ <| |___
 *                             /___|/___/|_| /_/_____|
 *
 * $Id: multi-post.c,v 1.1 2002/05/06 13:38:28 bagder Exp $
 *
 * This is an example application source code using the multi interface
 * to do a multipart formpost without "blocking".
 */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <winsock2.h>

#include "curl.h"

#define TOHEX(x) ((x)>9 ? (x)+55 : (x)+48)
void URLEncode(char* szIn, char** pOut);

int main(int argc, char *argv[])
{
  CURL *curl;
//  CURLcode res;

  CURLM *multi_handle;
  int still_running;

  struct HttpPost *formpost=NULL;
  struct HttpPost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  char buf[] = "Expect:";

  char name[] = "進步.txt";
  char* pUrlName = NULL;
  URLEncode(name, &pUrlName);

  curl_formadd((curl_httppost**)&formpost,
               (curl_httppost**)&lastptr,
               CURLFORM_COPYNAME, "__VIEWSTATE",
               CURLFORM_COPYCONTENTS, "dDw4ODE3NDk3ODk7Oz7wjZQSjAs1UJPOWebtakpkNfcu/w==",//old
//               CURLFORM_COPYCONTENTS, "dDw4ODE3NDk3ODk7Oz5EO36hoeY0AjmXXbTuRliuM0KJ3w==",//new
//               CURLFORM_COPYCONTENTS, "dDw4ODE3NDk3ODk7Oz7PeymyMHBp1qNYBrSPBdI2MqFLRw==",//new
               CURLFORM_END);

  /* Fill in the file upload field */
  curl_formadd((curl_httppost**)&formpost,
               (curl_httppost**)&lastptr,
               CURLFORM_COPYNAME, "File1",
               CURLFORM_FILE, "d://進步.txt",
               CURLFORM_FILENAME, pUrlName,
               CURLFORM_END);

/*  curl_formadd((curl_httppost**)&formpost,
               (curl_httppost**)&lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_FILENAME, "tt.bmp",
               CURLFORM_END);
*/
  /* Fill in the filename field */
  curl_formadd((curl_httppost**)&formpost,
               (curl_httppost**)&lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_COPYCONTENTS, "tt.bmp",
               CURLFORM_END);


  /* Fill in the submit field too, even if this is rarely needed */
  curl_formadd((curl_httppost**)&formpost,
               (curl_httppost**)&lastptr,
               CURLFORM_COPYNAME, "CmdUpload",
               CURLFORM_COPYCONTENTS, "UpLoad",
               CURLFORM_END);

  curl = curl_easy_init();
  multi_handle = curl_multi_init();

  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl && multi_handle) {
    int perform=0;

    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://192.168.160.99/FileUploadDemo.aspx");

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

    curl_multi_add_handle(multi_handle, curl);

    while(CURLM_CALL_MULTI_PERFORM ==
          curl_multi_perform(multi_handle, &still_running));

    while(still_running) {
      struct timeval timeout;
      int rc; /* select() return code */

      fd_set fdread;
      fd_set fdwrite;
      fd_set fdexcep;
      int maxfd;

      FD_ZERO(&fdread);
      FD_ZERO(&fdwrite);
      FD_ZERO(&fdexcep);

      /* set a suitable timeout to play around with */
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;

      /* get file descriptors from the transfers */
      curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

      switch(rc) {
      case -1:
        /* select error */
        break;
      case 0:
        printf("timeout!/n");
      default:
        /* timeout or readable/writable sockets */
        printf("perform!/n");
        while(CURLM_CALL_MULTI_PERFORM ==
              curl_multi_perform(multi_handle, &still_running));
        printf("running: %d!/n", still_running);
        break;
   }
    }

    curl_multi_cleanup(multi_handle);

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree((curl_httppost*)formpost);

    /* free slist */
    curl_slist_free_all (headerlist);
  }
  delete[] pUrlName;
  return 0;
}

void URLEncode(char* szIn, char** pOut)
{
 int nInLenth = strlen(szIn);
 int nFlag = 0;
 BYTE byte;
 *pOut = new char[nInLenth*3];
 char* szOut = *pOut;
 for (int i=0; i<nInLenth; i++)
 {
  byte = szIn[i];
  if (isalnum(byte))
  {
   szOut[nFlag++] = byte;
  }
  else
  {
   if (isspace(byte))
   {
    szOut[nFlag++] = '+';
   }
   else
   {
    szOut[nFlag++] = '%';
    szOut[nFlag++] = TOHEX(byte>>4);
    szOut[nFlag++] = TOHEX(byte%16);
   }
  }
 }
 szOut[nFlag] = '/0';
}


相關的html程式碼如下:

<%@Page Language="C#"%>
<HTML>
 <HEAD>
  <TITLE>
   File Uploading in ASP.Net Using C# - Demo
  </TITLE>
 </HEAD>
<BODY>
<script language="C#" runat="Server">
 //Event Handler for the upload button
 void UploadFile(object Sender,EventArgs E)
 {
  if (File1.PostedFile !=null) //Checking for valid file
  {  
   // Since the PostedFile.FileNameFileName gives the entire path we use Substring function to rip of the filename alone.
   string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("//") + 1) ;
   string StrFileType = File1.PostedFile.ContentType ;
   int IntFileSize =File1.PostedFile.ContentLength;
   //Checking for the length of the file. If length is 0 then file is not uploaded.
   if (IntFileSize <=0)
    Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>");
   else
   {
    File1.PostedFile.SaveAs(Server.MapPath(".//" + StrFileName));
    Response.Write( "<font color='green' size='2'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " bytes was uploaded successfully</font>");
   }
  }
 }
</script>
<%if(!Page.IsPostBack)
{
%>
<H2 align="center">File uploading in ASP.Net using C# - Demo</H2>
<!-- Declaration of server side form. Note the enctype attribute of the form has to be set to multipart/form-data -->
<form id="FrmFileUploadDemo" name="FrmFileUploadDemo"  method="post" enctype="multipart/form-data" runat="server" >
 <TABLE align="center" bgcolor="lightyellow">
  <TR>
   <TD>
    <font size="2">Select a file to upload</font> <input type="file" id="File1" name="File1" runat="server">
   </TD>
  <TR>
  <TR>
   <TD align="center">
    <asp:button value="Upload" runat="server" id="CmdUpload" Text="UpLoad" onClick="UploadFile" />
   </TD>
  </TR>
 </TABLE>
</form>
<%}%>


<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<font size="2">
Please feel to contact the author <a href="mailto:[email protected]">Sriram</a> for
your valuable suggestions and feedbacks.</font>

</BODY>
</HTML>

相關推薦

使用CURL檔案curl_formadd一個例子

 /***************************************************************************** *                                  _   _ ____  _      *  P

PHP7 cURL檔案

在做微信上傳素材檔案時出了點問題,伺服器提示media缺失,原上傳程式碼如下:function https_request($url,array $data = null) { $curl = curl_init(); curl_setopt(

php中curl檔案

    $ch=curl_init('http://localhost/post.php');         curl_setopt($ch, CURLOPT_HEADER, 0);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     // l

php7 curl檔案

php7使用curl上傳檔案返回錯誤碼26;程式碼如下: $size = filesize($path);//檔案大小 $ch = curl_init($url);

php如何CURL 檔案到其他伺服器

今天想用php curl上傳檔案到別的伺服器,百度了下找到一個方法, $ch = curl_init(); $data = array('name' => 'Foo', 'file' => '@/home/vagrant/test.png'); curl

php curl檔案

PHP5.6之前實現Example #2 上傳檔案 <?php /* http://localhost/upload.php: print_r($_POST); print_r($_FILES

php curl檔案$_FILES為空問題

php使用curl上傳檔案,程式碼如下: 傳送的程式碼(完全是官方的示例) <?php /* http://localhost/upload.php:print_r($_POST);print_r($_FILES);*/ $ch = curl_init(); $data

curl檔案的用法

用curl下載網頁估計大家都會,但是模擬 multipart/form-data 形式的 form 上傳檔案則稍稍複雜些。命令列如下: curl -F "[email protected]/home/test/file.tar.gz" http://localhost/action.php 如

curl傳送請求檔案(multipart file upload)

折騰一下午的問題 第三方介面需要我們傳multipart 上傳檔案 curl一直各種試不成功,用Restlet Client工具總是能成功! 對比傳送的頭,發現工具在Content-Type: multipart/form-data;後面多了個這個boundary 然後去查了下

通過okhttp3方式呼叫另一個專案檔案

OkHttp3檔案上傳介面   專案中用到需要將檔案上傳到另一專案B上,專案B提供了一個檔案上傳的介面。就用到了在後臺介面中傳送post請求來將檔案傳過去。可以使用okhttp3的方式,非常簡單。 1、加依賴   首先專案加如下依賴: <dependency>

curl post表單檔案(C++)

最近測試如何上傳檔案到伺服器。原來傳照片一致通過binary 形式傳檔案,或者把圖片base64編碼傳圖片。一致沒有用form-data 表單形式傳送資料,今天嘗試下如何使用libcurl提供的API上傳檔案。 Sample code: #include <

記錄一個解決了一個晚上加一個白天的問題,關於springMVC檔案的功能

在做檔案上傳功能,用到了springMVC的這個類 import org.springframework.web.multipart.MultipartFile; 但是不管前臺怎麼傳檔案的值都過不去,查了下需要在wen.xml 建立bean <bean

PHP使用CURL向Python,Golang傳送檔案表單檔案[HTTP協議下Api]

PHP傳送方程式碼段: <?php /** * htppCurl表單上傳檔案 * @param $file FILE_ADDR * @param string $url uri * @param string $key key * @return bool|mixed *

PHP:curl模擬form表單檔案

<form action="" method="post" enctype="multipart/form-data"> <input type="file" name="upload"> <buttion>submit</button> </f

一個簡單的WinHttp檔案的類

這幾天用WinHttp做一個檔案上傳的東東,中間遇到大坑,雖然時候發現其實是小問題,但是既然耽擱了這麼久的時間,還是決定寫下來。 之前也在網上看到過很多網友的文章,說在使用WinHttp上傳檔案的時候總是出現各種問題,其實,只要按照格式來,就不會有這麼難。這裡首先要曉得在使

在Vue中封裝一個檔案元件

封裝一個上傳檔案的元件,如下: 使用<input type='file'> 來實現檔案上傳,具體操作參照以往JS版的實現 這裡主要說作為一個元件,選中檔案之後,在輸入框中顯示檔名稱,點選Submit將將檔案傳給父元件,再由父元件提價到後臺

通過WebClient的Postdata將檔案功能放到另一個網站上去操作

看了本文的標題,可能很多人不太明白,其實這裡講的只是一個簡單的處理辦法。以前做的上傳檔案也是分離的,上傳後的檔案存在一個檔案伺服器上面,這個應該很多人都會。今天我這裡提的是分離上傳功能,因為上傳過程也是一個佔用伺服器資源的大問題,如果能夠把它和網站分開,哪效果應該會有很大的提

關於curl模擬檔案

在用curl模擬上傳的時候遇到一個小問題,所以記錄下來: 在 curl 中設定表單,包括檔案上傳網上很多都是這樣的 $post_data = array( "file" => "@" . "檔案所在路徑" ); $

SpringMVC檔案例子

伺服器: Apache Tomcat/8.0.33 開發環境:Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) Build id: 20160218-0600 java versi

JAVA_ 網路程式設計,寫一個可以檔案的伺服器和客戶端

服務端 class Server { public static void main(String[] args) throws Exception { //建立服務端Socket ServerS