我想使用网站的API上传文件(连续,不分块)。
以下是API的信息:
Request to: http://server/upv3.php
Required Vars:
[username] account username
[password] account password
[upload_session] upload session name from step 1 // I got that
[chunk_no] set to "1"
[chunk_number] set to "1"
[filesize] filesize of current file in bytes
[fn] filedata (in HTTP forms known as type="file")
[finalize] set to "1"
Response: DOWNLOAD-URL;FILESIZE;MD5
Example: http://www.url.com/dl/HJD74ZDM1;
6547231;316508123e89909723fe95945caf00a5
(linebreak only in example!)
我认为应该使用POST
发送所有这些属性。属性upload_session
是我可以成功请求的属性。
这是我的实施:
public void SOUpload()
{
string soUploadSession = "";
string soUploadServer = "";
SOCreateUploadSession(ref soUploadSession, ref soUploadServer);
HttpWebRequest request = (HttpWebRequest)(HttpWebRequest.Create("http://" + soUploadServer));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
long bytesRead = 0;
byte[] fileBuffer = new byte[file.fileSize];
FileStream fileStream = new FileStream("C:\\Users\\Patrick\\Downloads\\Prog\\UnityAssets\\start.unitypackage", FileMode.Open);
byte[] postData = Encoding.ASCII.GetBytes("&username=USERNAME" + "&password=PASSWORD" +
"&upload_session=" + soUploadSession +
"&chunk_no=1" + "&chunk_number=1" +
"&filesize=" + file.fileSize.ToString() +
"&finalize=1" + "&fn=");
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
while ((bytesRead = fileStream.Read(fileBuffer, 0, fileBuffer.Length)) != 0)
{
requestStream.Write(fileBuffer, 0, (int)bytesRead);
}
fileStream.Close();
requestStream.Close();
Stream responseStream = request.GetResponse().GetResponseStream();
byte[] responseBuffer = new byte[1024];
responseStream.Read(responseBuffer, 0, responseBuffer.Length);
string response = Encoding.ASCII.GetString(responseBuffer);
}
我知道如何实现下载(使用octet-stream),所以我认为上传大致相同,主要区别在于不是从网络流中读取数据并将其写入文件,我正在从文件流中读取数据并将此数据写入网络流....但显然它不会那样工作。