|
-
July 24th, 2012, 06:54 PM
#1
[SOLVED]VB + ASP -> C++ + libCURL
Hi, ALL,
I am trying to convert VB code to the C++ and libCURL.
Here is the server part in asp:
Default.aspx
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div>
<form id="Form2" Method="Post" EncType="Multipart/Form-Data" RunAt="Server">
<%-- The File upload Html control --%>
Choose Your File To Upload <BR>
<Input Name="MyFile" id="MyFile" Type="File" RunAt="Server">
<BR>
<%-- A button - when clicked the form is submitted and the
Upload_Click event handler is fired... --%>
<Input id="Submit1" Type="Submit" Value="Upload"
RunAt="Server">
</form> </div>
</body>
</html>
Default.aspx.vb
Code:
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Page.IsPostBack Then
'Grab the file name from its fully qualified path at client
Dim strFileName As String = MyFile.PostedFile.FileName
'Response.Write(strFileName)
' only the attched file name not its path
Dim c As String = System.IO.Path.GetFileName(strFileName)
'Save uploaded file to server at C:\ServerFolder\
Try
MyFile.PostedFile.SaveAs("C:\inetpub\wwwroot\<my_path>\files\" + c)
Catch Exp As Exception
End Try
And here is the relevant part of the client code:
Code:
HttpUploadFile("http://xxx.xxx.xxx.xxx/<my_path>/Default.aspx", "C:\temp\12345 .bmp", "MyFile", "image/bitmap")
Private Sub HttpUploadFile( _
ByVal uri As String, _
ByVal filePath As String, _
ByVal fileParameterName As String, _
ByVal contentType As String)
'ByVal otherParameters As Specialized.NameValueCollection)
' This routine takes the locally generated image and uploads it using HTTP Post data
Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
Dim newLine As String = System.Environment.NewLine
Dim boundaryBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
request.ContentType = "multipart/form-data; boundary=" & boundary
request.Method = "POST"
request.KeepAlive = True
request.Credentials = Net.CredentialCache.DefaultCredentials
Using requestStream As IO.Stream = request.GetRequestStream()
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}"
Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
Dim headerBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(header)
requestStream.Write(headerBytes, 0, headerBytes.Length)
However using libCURL I'm getting the "Object reference is not set to an instance of the object", because the server code does not have "MyFile = new InputText" line. I was told from the author that "MyFile" object has been created on the client side by the "headerTemplate" variable.
Is libCURL capable of doing something like this?
Here is my code in C++/wxWidgets/libCURL:
Code:
CURLcode error;
char errorMsg[1024];
struct curl_httppost *first = NULL, *last = NULL;
struct curl_slist *post = NULL;
wxString header = wxString::Format( "Content-Disposition: form-data; name=\"MyFile\"; filename=\"%s\"", filename;
post = curl_slist_append( post, (const char *) header.c_str() );
post = curl_slist_append( post, "Content-Type: image/bitmap" );
curl_formadd( &first, &last, CURLFORM_COPYNAME, "name", CURLFORM_FILE, (const char *) fileName.c_str(), CURLFORM_END );
curl_formadd( &first, &last, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, (const char *) fileName.c_str(), CURLFORM_END );
curl_formadd(&first, &last, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END );
CURL *handle = wxGetApp().GetCurlHandle();
FILE *fp = fopen( "session.log", "w+" );
curl_easy_setopt( handle, CURLOPT_VERBOSE, 1L );
curl_easy_setopt( handle, CURLOPT_HEADER, 1L );
curl_easy_setopt( handle, CURLOPT_STDERR, fp );
curl_easy_setopt( handle, CURLOPT_ERRORBUFFER, errorMsg );
curl_easy_setopt( handle, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/<my_path>/Default.aspx" );
curl_easy_setopt( handle, CURLOPT_HTTPPOST, first );
curl_easy_setopt( handle, CURLOPT_HTTPHEADER, post );
error = curl_easy_perform( handle );
curl_slist_free_all( post );
curl_formfree( first );
curl_easy_setopt( handle, CURLOPT_HTTPPOST, NULL );
curl_easy_setopt( handle, CURLOPT_HTTPHEADER, NULL );
curl_easy_setopt( handle, CURLOPT_HTTPGET, 1L );
curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, CWindowPanel::get_data );
curl_easy_setopt( handle, CURLOPT_WRITEDATA, &m_stringToRead );
error = curl_easy_perform( handle );
curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, NULL );
curl_easy_setopt( handle, CURLOPT_WRITEDATA, NULL );
What am I doing wrong?
Thank you.
Last edited by OneEyeMan; July 28th, 2012 at 01:17 PM.
Reason: Mark as solved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|