概述
/*****************************************************************************
* _ _ ____ _
* 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:nilapenn@hotmail.com">Sriram</a> for
your valuable suggestions and feedbacks.</font>
</HTML>
最后
以上就是羞涩秋天为你收集整理的使用CURL上传文件curl_formadd的一个例子的全部内容,希望文章能够帮你解决使用CURL上传文件curl_formadd的一个例子所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复