I want to upload a file (continuously, not chunked) using the site's API.
Here are the information of the 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!)
I think all this properties should be send using POST
. The Attribute upload_session
is a property I can request successfully.
Here's my implementation:
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);
}
I know how I can implement a download (using octet-stream), so I thought an upload is mostly the same, the main-difference is that instead of reading data from the network-stream and writing this into a file, I am reading data from the file-stream and writing this data to an network-stream....but obviously it does not work that way.