Home > Back-end >  Use idhttp controls, use baidu cloud API development, upload a file
Use idhttp controls, use baidu cloud API development, upload a file

Time:09-25

Recently asked software can save the file to the user in the network location, directly find the baidu cloud API test first, because there is no the SDK package with Delphi, baidu cloud API can oneself according to API format, or get the host data, can now be empowered, query quota, reading list, and so on, but not always upload file, I use indy10 idhttp control, post data has been wrong, API specification is as follows:

Baidu network location to upload a single file API:
upload a single file
API demo
The function
Upload a single file,
Baidu PCS service is now the biggest 2 g support a single file upload,
If you want to support very large files (& gt; 2 g) breakpoint continuingly, please refer to the following file upload "divided" method,

The HTTP request method
POST
URL
https://c.pcs.baidu.com/rest/2.0/pcs/file
Note:
Compatible with the original domain name pcs.baidu.com; Using the new domain name c.pcs.baidu.com, provides faster, more stable upload service,
Request parameter
Whether the parameter name type required description
Method the string is a fixed value, the upload,
Access_token access identifier string is the developer must use HTTPS calls,
The path string is upload file path (including upload file name),
Note:
Path length limit of 1000
Path cannot contain the following characters: \ \? | "& gt; <: *
Filename or path name begin ending is not ". "or blank characters, white space characters include: \ r \ n \ t, Spaces,, \ \ 0 x0B
File char [] is the content of the upload file,
Whether ondup string
Overwrite: said cover file with the same;
Newcopy: copy and rename, naming rules for the "file name _ date. The suffix",
Note:
The above four parameters in the file needs to pass through the POST form, other parameters must be passed through the query_string,
Return parameter
The parameter name type UrlEncode description
The path string is absolute path to the file,
The size size bytes uint64 no file,
Ctime uint64 no file creation time,
Mtime uint64 no file modification time,
Md5 string no file md5 signatures,
Fs_id uint64 unique ID, no files in the PCS of temporary
Example
Sample request:
POST https://pcs.baidu.com/rest/2.0/pcs/file? Method=upload& Path=% 2 fapps % 2 falbum % 2 f1. JPG& Access_token=b778fb598c717c0ad7ea8c97c8f3a46f
Response example:
{
"Path" : "/apps/album/1. JPG",
"Size" : 372121,
"Ctime" : 1234567890,
"Mtime" : 1234567890,
"Md5" : "cb123afcc12453543ef,"
"Fs_id" : 12345,
"Request_id" : 4043312669
}


Use idhttp code is as follows:
IdHTTP1. ProtocolVersion:=pv1_1;
IdHTTP1. HandleRedirects:=True;
Idhttp1. IOHandler:=IdSSLIOHandlerSocketOpenSSL1;
IdHTTP1. Request. UserAgent:='Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; The.net CLR 2.0.50727) ';
IdHTTP1. HTTPOptions:=IdHTTP1. HTTPOptions + [hoKeepOrigProtocol];

IdStream:=TIdMultiPartFormDataStream. Create;
Response:=TStringStream. Create (");

SURL:='https://pcs.baidu.com/rest/2.0/pcs/file? Method=upload& Path=% 2 fapps % 2 fswrtest '
+ '% 2 f1053. Txt& Access_token=3.897697 a4f3562f2c1be2744a5847cbf9. 2592000. '
1380763835.1093865639-1269890 '+';

IdStream. AddFile (' file1 ', 'c: \ 1213. TXT', 'multipart/form - data);
Idhttp1. Request. ContentType:=idStream RequestContentType;
IdStream. Position:=0;
Idhttp1. Post (sURL, idStream, response);

Since I have been sent an error 'HTTP1.1/403 Forbidden, need to upload a file, and hope to have the experience of the boss to give directions!!!!!

CodePudding user response:

No one to answer it, should be post the file to the time of this site has a problem, don't know how to solve

CodePudding user response:

Could it be (utf) coding problem?

CodePudding user response:

Code should be how to test, now is to upload a file, the API instructions is upload file into a byte stream, did not say what code

CodePudding user response:

O master, online, etc

CodePudding user response:

Find a Java code, can someone help me translated into Delphi

[Java] view plaincopyprint?/* *
* @ param path cloud disk storage path
* @ param name to upload the file
* @ return
* @ throws the Exception
*/
Public FileBase uploadFile (String path, the File File) throws the Exception {
//simulation file
The String fileName="README. Md";
The file=new file (fileName);
Path="% 2 fapps % 2 fmybaidu % 2 f";//I'm using the url encoded source for: - & gt; "/apps/mybaidu/
/"


//will need to pass url value of parameter and url assembled
The String u="HTTP://https://pcs.baidu.com/rest/2.0/pcs/file? Path="+ + file path. The getName () +" & amp; Method=upload& Access_token=your own token application value ";



PostMethod filePost=new PostMethod (u);
//the parameters of the post submission
Part [] parts={new FilePart (fileName, file)};
//set the multimedia parameters, works in a similar form form the enctype="multipart/form - data"
FilePost. SetRequestEntity (new MultipartRequestEntity (parts, filePost getParams ()));
HttpClient clients=new HttpClient ();
//response code
Int status=clients. ExecuteMethod (filePost);
System. The out. Println (" uploaded successfully "+ + fileName path);

BufferedReader buReader=new BufferedReader (new InputStreamReader (filePost. GetResponseBodyAsStream (), "utf-8"));
StringBuffer sb=new StringBuffer();
String line;
While ((line=buReader readLine ())!=null) {
Sb. Append (line);
}
BuReader. Close ();

//parsed into object
Gson Gson=new Gson ();
FileBase cloudInfo=gson. FromJson (sb. ToString (), FileBase. Class);

Return cloudInfo;
}

CodePudding user response:

To beg!

CodePudding user response:

Again under the roof, it doesn't have time to make this a few days, no one back

CodePudding user response:

Ah, no one back

CodePudding user response:

Ah, no one how to knot stick back

CodePudding user response:

403 should be the problem of the URL path, but should be no problem,,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related