get PATH_INFO or PATH_TRANSLATED failed.
\n"); + return cgiFormNotFound; + } + + char stdScriptPath[256]; + HGBase_StandardiseFileName(scriptPath, stdScriptPath, 256); + char* p = strstr(scriptFullPath, stdScriptPath); + if (NULL == p) + { + fprintf(cgiOut, "PATH_INFO or PATH_TRANSLATED error.
\n"); + return cgiFormNotFound; + } + + std::string rootPath(scriptFullPath, p - scriptFullPath); + + fprintf(cgiOut, "rootPath: "); + cgiHtmlEscape(rootPath.c_str()); + fprintf(cgiOut, "no filename.
\n"); + return cgiFormNoFileName; + } + + fprintf(cgiOut, "filename: "); + cgiHtmlEscape(name); + fprintf(cgiOut, "bad file size.
\n"); + return cgiFormEmpty; + } + + fprintf(cgiOut, "file size: %d bytesno remote filename.
\n"); + return cgiFormNoFileName; + } + + fprintf(cgiOut, "remote_filepath: "); + cgiHtmlEscape(remoteFilePath); + fprintf(cgiOut, "Could not open the file.
\n"); + return cgiFormOpenFailed; + } + + std::string savePath = rootPath + remoteFilePath; + char stdSavePath[256]; + HGBase_StandardiseFileName(savePath.c_str(), stdSavePath, 256); + char stdSaveDir[256]; + HGBase_GetFilePath(stdSavePath, stdSaveDir, 256); + HGBase_CreateDir(stdSaveDir); + + fprintf(cgiOut, "savePath: "); + cgiHtmlEscape(stdSavePath); + fprintf(cgiOut, "create file failed.
\n"); + cgiFormFileClose(file); + return cgiFormOpenFailed; + } + + char tmp[1024]; + int got = 0; + while (cgiFormFileRead(file, tmp, 1024, &got) == cgiFormSuccess) + { + fwrite(tmp, got, sizeof(char), fd); //把读出的content写入新文件 + } + + fclose(fd); + cgiFormFileClose(file); + + //打印输出 + fprintf(cgiOut, "upload file success.
\n"); + return cgiFormSuccess; +} \ No newline at end of file diff --git a/sdk/webservice/ManagerV2.cpp b/sdk/webservice/ManagerV2.cpp index ba285168..827babbc 100644 --- a/sdk/webservice/ManagerV2.cpp +++ b/sdk/webservice/ManagerV2.cpp @@ -2830,7 +2830,41 @@ namespace ver_2 int ManagerV2::HttpUpload(const std::string& host, int port, const std::string& path, const std::string& filePath, const std::string& remoteFilePath) { - return -1; + curl_global_init(CURL_GLOBAL_ALL); + + int ret = -1; + CURL* curl = curl_easy_init(); + if (curl) + { + struct curl_httppost* formpost = NULL; + struct curl_httppost* lastptr = NULL; + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_FILE, filePath.c_str(), CURLFORM_END); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "remote_filename", CURLFORM_COPYCONTENTS, remoteFilePath.c_str(), CURLFORM_END); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END); + + char url[512]; + sprintf(url, "http://%s:%d%s", host.c_str(), port, path.c_str()); + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + + /* Perform the request, res will get the return code */ + CURLcode res = curl_easy_perform(curl); + /* Check for errors */ + if (res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res)); + else + ret = 0; + + /* always cleanup */ + curl_easy_cleanup(curl); + + /* then cleanup the formpost chain */ + curl_formfree(formpost); + } + + curl_global_cleanup(); + return ret; } static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* stream) @@ -2870,22 +2904,6 @@ namespace ver_2 CURL* curl = curl_easy_init(); if (NULL != curl) { - //char tmpName[256]; - //HGBase_GetUuid(tmpName, 256); - char remotePath[256]; - HGBase_GetFilePath(remoteFilePath.c_str(), remotePath, 256); - char remoteName[256]; - HGBase_GetFileName(remoteFilePath.c_str(), remoteName, 256); - - //char ftp_rnfr[512]; - //sprintf(ftp_rnfr, "RNFR %s", tmpName); - //char ftp_rnto[512]; - //sprintf(ftp_rnto, "RNTO %s", remoteName); - - //struct curl_slist* headerlist = NULL; - //headerlist = curl_slist_append(headerlist, ftp_rnfr); - //headerlist = curl_slist_append(headerlist, ftp_rnto); - /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); @@ -2894,12 +2912,12 @@ namespace ver_2 char url[512]; if (!user.empty() && !password.empty()) { - sprintf(url, "ftp://%s:%s@%s:%d%s%s", user.c_str(), password.c_str(), - host.c_str(), port, remotePath, remoteName); + sprintf(url, "ftp://%s:%s@%s:%d%s", user.c_str(), password.c_str(), + host.c_str(), port, remoteFilePath.c_str()); } else { - sprintf(url, "ftp://%s:%d%s%s", host.c_str(), port, remotePath, remoteName); + sprintf(url, "ftp://%s:%d%s", host.c_str(), port, remoteFilePath.c_str()); } curl_easy_setopt(curl, CURLOPT_URL, url); diff --git a/test/webservice/demo.html b/test/webservice/demo.html index 267b416b..563949a5 100644 --- a/test/webservice/demo.html +++ b/test/webservice/demo.html @@ -360,6 +360,18 @@ })); } + function HttpUpload() + { + socket.send(JSON.stringify({ + 'func':'upload_local_file', + 'file_path':'D:\\1.jpg', + 'remote_file_path':'/savedir/123.jpg', + 'upload_mode':'http', + 'http_host':'192.168.100.148', + 'http_port':8001 + })); + } + function MergeLocalImage() { socket.send(JSON.stringify({ @@ -831,10 +843,13 @@ + + + @@ -876,6 +891,7 @@ + diff --git a/third_party/cgic/cgic.c b/third_party/cgic/cgic.c index 0af957f5..f7b9f0e7 100644 --- a/third_party/cgic/cgic.c +++ b/third_party/cgic/cgic.c @@ -31,7 +31,7 @@ #include