當前位置:首頁 » 文件管理 » cpost上傳圖片

cpost上傳圖片

發布時間: 2023-01-12 23:33:26

⑴ 如何在 python 中模擬 post 表單來上傳文件

在機器上安裝了Python的setuptools工具,可以通過下面的命令來安裝 poster:

easy_installposter

裝完之後,安裝下面代碼就可以實現post表單上傳文件了:

fromposter.encodeimportmultipart_encode
fromposter.streaminghttpimportregister_openers
importurllib2

#在urllib2上注冊http流處理句柄
register_openers()

#開始對文件"DSC0001.jpg"的multiart/form-data編碼
#"image1"是參數的名字,一般通過HTML中的<input>標簽的name參數設置

#headers包含必須的Content-Type和Content-Length
#datagen是一個生成器對象,返回編碼過後的參數
datagen,headers=multipart_encode({"image1":open("DSC0001.jpg","rb")})

#創建請求對象(localhost伺服器IP地址,5000伺服器埠)
request=urllib2.Request("http://localhost:5000/upload_image",datagen,headers)
#實際執行請求並取得返回
printurllib2.urlopen(request).read()

⑵ python requests 使用post方式上傳圖片

他那個文件是在當前目錄下, 所以不需要寫絕對路徑.

你只需要把第二行的files改成如下就好:

files={'file':open('/c/1/2.jpg','rb')}

⑶ C語言如何實現POST數據包

先建立TCP連接,然後發送HTTPpost.
你看看下面的數據包就知道了。

⑷ C++ 以POST方式向網頁提交數據.有錯誤。。求大神解救就這點財富值了,見諒。圖片是錯誤。

只幫你解決崩潰的問題哈


就說兩個問題:

1、VS2005或以上的IDE默認是用UNICODE的,你代碼里混用ANSI和UNICODE,又做強制轉換,導致了一些參數出錯


2、OpenRequest接受兩種不同的參數的,既然你用的是HTTP_VERB_POST,為什麼要強制轉換成LPCTSTR呢,這樣會導致它調用的是另外一個函數(C++支持同名函數,不同參數)


boolPostHttpPage(conststd::wstring&hostName,
conststd::wstring&pathName,
conststd::wstring&postData)
{
usingnamespacestd;
CInternetSessionsession(_T("session"),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,
NULL,INTERNET_FLAG_DONT_CACHE);//設置不緩沖

INTERNET_PORTnPort=80;
DWORDdwRet=0;

CHttpConnection*pServer=session.GetHttpConnection(
(LPCTSTR)hostName.c_str(),nPort);
CHttpFile*pFile=pServer->OpenRequest(CHttpConnection::
HTTP_VERB_POST,(LPCTSTR)pathName.c_str());

CStringstrHeaders=L"Content-Type:application/x-www-form-urlencoded";//請求頭
//開始發送請求

pFile->SendRequest(strHeaders,(LPVOID)postData.c_str(),
postData.size());
pFile->QueryInfoStatusCode(dwRet);

if(dwRet==HTTP_STATUS_OK)
{
CStringresult,newline;

while(pFile->ReadString(newline))
{//循環讀取每行內容
result+=newline+L" ";
}

std::cout<<result<<std::endl;//顯示返回內容
}
else
{
returnfalse;
}
deletepFile;
deletepServer;
cout<<"成功"<<endl;

session.Close();

returntrue;
}

//調用時用下面的代替
PostHttpPage(L"localhost",L"welcome.php",L"name=rain&age=21");

⑸ 怎麼用C實現Http POST功能向Http伺服器上傳文件

用socket就行了。
和伺服器建立請求。
然後發送請求報文"\r\n\r\n"結束之後是數據。
post分為,application/x-www-form-urlencoded和multipart/form-data boundary=
要是上傳文件,就得使用multipart/form-data boundary=...
伺服器那邊根據boundary來解析出數據。

⑹ 易語言用伺服器組件怎麼接收POST過來的圖片

在Content-Type: image/jpeg這一行的下面就是圖片的數據。
通過分割字元串或者位元組集,將這一行下面的內容與邊界——即下一個 ------ 或EOF之間的內容取出,單獨寫入一個文件,就可以得到文件。
其實易語言不大適合在網頁伺服器用,因為它的使用真的不算方便。

⑺ C++Builder XE2怎麼通過IDHTTP的POST方法上傳文件

C++Builder XE2如何通過IDHTTP的POST方法上傳文件?
DEPHI的代碼如下:

var
MultiPartFormDataStream: TMsMultiPartFormDataStream;
idhttp1:TIdHTTP;
begin
MultiPartFormDataStream := TMsMultiPartFormDataStream.Create;
try
id(nil);
IdHTTP1.ProtocolVersion:=pv1_1;
IdHTTP1.HandleRedirects := True;
IdHTTP1.Request.UserAgent:='Klive';
IdHTTP1.Request.Connection:='Keep-Alive';
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;

MultiPartFormDataStream.AddFormField('name', 'file');
MultiPartFormDataStream.AddFormField('filename', Path);
MultiPartFormDataStream.AddFile('file', filename, 'testmime');
MultiPartFormDataStream.PrepareStreamForDispatch;
MultiPartFormDataStream.Position := 0;

try
Result:=pchar(id(str,MultiPartFormDataStream));
except
on e: EIdHTTPProtocolException do
begin
result:= pchar(e.ErrorMessage);// Application.MessageBox(PChar(e.ErrorMessage), 'ERROR', MB_OK + MB_ICONSTOP);
end;
end;

finally
MultiPartFormDataStream.Free;

這個代碼是金山快盤上傳文件的實現代碼,具體要求是這樣的:

POST /cdlnode/1/fileops/upload_file oauth_nonce=57895862&oauth_timestamp=1328956550&oauth_consumer_key=xcKXXgo9AkXdJuBn&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=&oauth_signature=eQ9BhjCaIpUAWDD3cbb%2BrtlfXt4%3D&path=%2Fttt1328956550.95sss%2Ftestw.wps&root=app_folder&overwrite=True HTTP/1.1
Accept-Encoding: identity
Content-Length: 237
Host: ufaclien
Content-Type: multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$
Connection: close
User-Agent: klive

------------ThIs_Is_tHe_bouNdaRY_$
Content-Disposition: form-data; name="file"; filename="testw.wps"
Content-Type: application/octet-stream

1328956550.991328956550.991328956550.991328956550.99
------------ThIs_Is_tHe_bouNdaRY_$--

⑻ 如何用C++或者python實現接收客戶端發送的post請求

一個http請求包括三個部分,分別為請求行,請求報頭(請求頭),消息主體(請求體),類似以下這樣:

{
「args」:{},
「data」:「」,
「files」:{
「file」:「Helloworld!」
},
「form」:{},
「headers」:{……
「Content-Type」:「multipart/form-data;boundary=」,
……
},
「json」:null,
……
}
---------------------
作者:weixin_40283480
來源:CSDN
原文:https://blog.csdn.net/weixin_40283480/article/details/79208413
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

⑼ 如何使用multipart/form-data格式上傳文件

在網路編程過程中需要向伺服器上傳文件。Multipart/form-data是上傳文件的一種方式。

Multipart/form-data其實就是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件後添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到伺服器。


表單形式上傳附件

具體的步驟是怎樣的呢?

首先,客戶端和伺服器建立連接(TCP協議)。

第二,客戶端可以向伺服器端發送數據。因為上傳文件實質上也是向伺服器端發送請求。

第三,客戶端按照符合「multipart/form-data」的格式向伺服器端發送數據。

既然Multipart/form-data格式就是瀏覽器用表單提交數據的格式,我們就來看看文件經過瀏覽器編碼後是什麼樣子。

這行指出這個請求是「multipart/form-data」格式的,且「boundary」是 「---------------------------7db15a14291cce」這個字元串。

不難想像,「boundary」是用來隔開表單中不同部分數據的。例子中的表單就有 2 部分數據,用「boundary」隔開。「boundary」一般由系統隨機產生,但也可以簡單的用「-------------」來代替。

實際上,每部分數據的開頭都是由"--" + boundary開始,而不是由 boundary 開始。仔細看才能發現下面的開頭這段字元串實際上要比 boundary 多了個 「--」

緊接著 boundary 的是該部分數據的描述。

接下來才是數據。


「GIF」gif格式圖片的文件頭,可見,unknow1.gif確實是gif格式圖片。

在請求的最後,則是 "--" + boundary + "--" 表明表單的結束。

需要注意的是,在html協議中,用 「 」 換行,而不是 「 」。

下面的代碼片斷演示如何構造multipart/form-data格式數據,並上傳圖片到伺服器。

//---------------------------------------

// this is the demo code of using multipart/form-data to upload text and photos.

// -use WinInet APIs.

//

//

// connection handlers.

//

HRESULT hr;

HINTERNET m_hOpen;

HINTERNET m_hConnect;

HINTERNET m_hRequest;

//

// make connection.

//

...

//

// form the content.

//

std::wstring strBoundary = std::wstring(L"------------------");

std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");

wstrHeader += strBoundary;

HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(), DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);

//

// "std::wstring strPhotoPath" is the name of photo to upload.

//

//

// uploaded photo form-part begin.

//

std::wstring strMultipartFirst(L"--");

strMultipartFirst += strBoundary;

strMultipartFirst += L" Content-Disposition: form-data; name="pic"; filename=";

strMultipartFirst += L""" + strPhotoPath + L""";

strMultipartFirst += L" Content-Type: image/jpeg ";

//

// "std::wstring strTextContent" is the text to uploaded.

//

//

// uploaded text form-part begin.

//

std::wstring strMultipartInter(L" --");

strMultipartInter += strBoundary;

strMultipartInter += L" Content-Disposition: form-data; name="status" ";

std::wstring wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));

// add text content to send.

strMultipartInter += wstrPostDataUrlEncode;

std::wstring strMultipartEnd(L" --");

strMultipartEnd += strBoundary;

strMultipartEnd += L"-- ";

//

// open photo file.

//

// ws2s(std::wstring)

// -transform "strPhotopath" from unicode to ansi.

std::ifstream *pstdofsPicInput = new std::ifstream;

pstdofsPicInput->open((ws2s(strPhotoPath)).c_str(), std::ios::binary|std::ios::in);

pstdofsPicInput->seekg(0, std::ios::end);

int nFileSize = pstdofsPicInput->tellg();

if(nPicFileLen == 0)

{

return E_ACCESSDENIED;

}

char *pchPicFileBuf = NULL;

try

{

pchPicFileBuf = new char[nPicFileLen];

}

catch(std::bad_alloc)

{

hr = E_FAIL;

}

if(FAILED(hr))

{

return hr;

}

pstdofsPicInput->seekg(0, std::ios::beg);

pstdofsPicInput->read(pchPicFileBuf, nPicFileLen);

if(pstdofsPicInput->bad())

{

pstdofsPicInput->close();

hr = E_FAIL;

}

delete pstdofsPicInput;

if(FAILED(hr))

{

return hr;

}

// Calculate the length of data to send.

std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);

std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);

std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);

int cSendBufLen = straMultipartFirst.size() + nPicFileLen + straMultipartInter.size() + straMultipartEnd.size();

// Allocate the buffer to temporary store the data to send.

PCHAR pchSendBuf = new CHAR[cSendBufLen];

memcpy(pchSendBuf, straMultipartFirst.c_str(), straMultipartFirst.size());

memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf, nPicFileLen);

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen, straMultipartInter.c_str(), straMultipartInter.size());

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen + straMultipartInter.size(), straMultipartEnd.c_str(), straMultipartEnd.size());

//

// send the request data.

//

HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)

⑽ 怎麼把電腦本地磁碟C:上的圖片傳到網上

你用免費的相冊阿,搜索「免費相冊」,注冊一個就好了
怎麼把圖片變成貼圖?
首先,電腦內存放的圖片不能上傳。所以你要找一個網路相冊把你要的圖片傳上去。比如網路空間的相冊,QQ空間的相冊,網易相冊等等。不過一般這些相冊上的照片只能在該網站的論壇上發,比如網路相冊上的只能發到網路帖吧,而如果發到其他網站就看不到圖片。接下來的步驟:·前兩項參考網路·1.將滑鼠移動到圖片上,點擊滑鼠右鍵,選擇「屬性」。
2.這時會出現一個對話框,在對話框「地址(URL)」一項旁邊,有一段很長的地址。將這段地址「復制」下來。3.將地址粘貼到帖地址的地方,之後點「確定「,完成帖圖的操作。你也可以直接看網路帖吧對這方面的介紹。地址:
http://www..com/search/post_img.html#n4[
把上面一段地址選中,復制,粘貼到窗口上方地址欄里,點「轉到」就或者按Enter回車鍵就可以看了]在其他網站上帖圖的方法和網路類似。介紹幾個可以上傳圖片的網站

http://www.freep.cn/
隨意貼
http://www.namipan.com/index.php
納米盤
http://tu.6.cn/
快捷貼圖
http://www.my2cool.com/asp/index.asp
網站名稱:my2Cool
http://97city.com/97city/001.html
網站名稱:97city(這個是英文的)

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:713
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:980
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:687
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:840
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:747
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1086
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:314
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:194
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:883
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:841