UploadFile

Description

This is the simplest way to upload a document to Docstoc. Note that not all parameters are mandatory.

REST - HTTP POST

http://rest.docstoc.com/upload/UploadFile

POST /upload/UploadFile HTTP/1.1
User-Agent: Docstoc RestApiTester
Content-Type: multipart/form-data; boundary=----------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Host: rest.docstoc.com
Content-Length: 2103
Expect: 100-continue
Connection: Keep-Alive
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Key" 
[Key]
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Ticket" 
[Ticket]
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="ReturnType" 
XML
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="IsPrivate" 
true
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Anonymous" 
false
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="FileName" 
C:\Documents and Settings\andrew\Desktop\test.txt
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Tags" 
 
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Title" 
 
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Desc" 
 
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="Language" 
 
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="TopCat" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="SubCat" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="SubSubCat" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="License" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="IncludeInPortfolio" 
false
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="FolderID" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="TemplateID" 
0
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6
Content-Disposition: form-data; name="FileContent" filename="test.txt"
this is a test document
------------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6--
Expand

SOAP 1.2

POST /service.asmx HTTP/1.1
Host: soap.docstoc.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <UploadFile xmlns="http://ws.docstoc.com/">
      <Key>string</Key>
      <Ticket>string</Ticket>
      <FileContent>base64Binary</FileContent>
      <FileName>string</FileName>
      <Title>string</Title>
      <Tags>string</Tags>
      <TopCat>int</TopCat>
      <SubCat>int</SubCat>
      <SubSubCat>int</SubSubCat>
      <Language>string</Language>
      <Desc>string</Desc>
      <License>int</License>
      <IsPrivate>boolean</IsPrivate>
      <Anonymous>boolean</Anonymous>
      <TemplateID>int</TemplateID>
      <FolderID>int</FolderID>
      <IncludeInPortfolio>boolean</IncludeInPortfolio>
    </UploadFile>
  </soap12:Body>
</soap12:Envelope>
Expand

Parameters

Name Data Type Description Optional
Key String Application Key
Ticket String The ticket provided after calling AuthenticateUser
ReturnType String XML - Results are an XML document (default)
JSON - Results are in JSON format
Yes
FileContent Byte Array The content of the document
FileName String The filename including the file extension. The entire path of the file (that is C:\dir\file.pdf) can be included but not mandatory. Providing the path can be useful later when trying to uniquely identify the document
Title String If title is provided, the title will be presented instead of the filename Yes
Tags String Words/phrases separated by commas Yes
TopCat Integer The category id. Omit or provide as zero (0) if you do not want to categorize this document. To get the categories call the GetCategories function Yes
SubCat Integer The sub category id. Omit or provide as zero (0) if you do not want to sub categorize this document. To get the categories call the GetCategories function Yes
SubSubCat Integer The sub sub category id. Omit or provide as zero (0) if you do not want to sub categorize this document. To get the categories call the GetCategories function Yes
Language String The language the document is in. If English, omit, provide as zero (0) or “English”. To get the list of languages call the GetLanguages function Yes
Desc String Document description/overview Yes
License Integer Must be provided as one of the following:
0 - PublicDomain
1 - Attribution
2 - AttributionNoDerivs
3 - AttributionShareAlike
4 - AttributionNonCommercial
5 - AttributionNonCommercialShareAlike
6 - AttributionNonCommercialNoDerivs
7 - TraditionalCopyright
Yes
IsPrivate Boolean True – document is private
False – document is public
Anonymous Boolean True – upload the file as an anonymous user
False – set the owner of the file to be the user authenticated by this ticket (default)
Yes
TemplateID Integer 0 – profile centric document template
1 - wide screen document template
FolderId Integer Id of the folder this document should be under. Omit or provide as zero (0) to upload the document under the My Document folder of this user. To retrieve a user's folder directory, call GetFolderTree. Yes
IncludeInPortfolio Boolean A portfolio refers to one of the tabs on the user's public profile page. Only the documents in a user's portfolio are shown when another user browses to the current user's profile page. Could be either of the following:
True - include in the user's portfolio
False - don't include in the user's portfolio (default)
Yes

Response

Condition Data Type Description
Success String
<Result>
	<DocumentId>DocId</DocumentId>
	<Key>Key</Key>
	<Password>Pass</Password>
</Result>
                
* Key and Pass are needed in order to access private documents
REST Failure HTTP Response Code See error codes section
SOAP Failure Web Exception See error codes section

Error Codes

Type Value Description
Response Code 400 Null ticket
Response Code 401 Invalid Ticket
Response Code 500 Invalid filetype
Response Code 500 File is too large
Response Code 500 File is too small
Response Code 500 Failure to convert file

Example

This is an example of how to use UploadFile using C#

private string UploadFileHttpRequest(string fileName)
{
    string output = string.Empty;
    MemoryStream postStream = null;
    BinaryWriter postWriter = null;
    HttpWebResponse response = null;
    StreamReader responseStream = null;
    
    const string CONTENT_BOUNDARY = "----------ae0cH2cH2GI3Ef1KM7GI3Ij5cH2gL6";
    const string CONTENT_BOUNDARY_PREFIX = "--";
    
    try
    {
        //put together url with querystring
        UriBuilder uriBuilder = new UriBuilder(_baseUrl + UPLOAD_FILE);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
        request.UserAgent = "REPLACE WITH YOUR USER AGENT";
        //30 seconds timeout
        request.Timeout = 300000;
        request.ContentType = "multipart/form-data; boundary=" + CONTENT_BOUNDARY;
        //create binary writer for the request stream
        postStream = new MemoryStream();
        postWriter = new BinaryWriter(postStream);
        //-- write out our form variables --
        //key
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Key\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(AUTH_KEY));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //ticket
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Ticket\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(AUTH_TICKET));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //return type
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"ReturnType\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(_returnType));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //is private
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"IsPrivate\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("false"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //anonymous
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Anonymous\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("false"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //file name
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"FileName\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(fileName));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //tags
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Tags\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(string.Empty));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //title
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Title\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(string.Empty));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //Desc
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Desc\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(string.Empty));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //Language
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"Language\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(string.Empty));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //Top category
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"TopCat\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //sub category
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"SubCat\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //sub sub category
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"SubSubCat\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //License
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"License\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //IncludeInPortfolio
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"IncludeInPortfolio\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("false"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //folder id
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"FolderID\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        //TemplateID
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"TemplateID\" \r\n\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("0"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        
        //read the file into a byte array
        byte[] fileContent = File.ReadAllBytes(fileName);
        
        //write the file header to the byte array
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "\r\n" +
        "Content-Disposition: form-data; name=\"FileContent\" " + 
        "filename=\"" + Path.GetFileName(fileName) + "\"\r\n\r\n"));
        //write the byte array into a stream
        postWriter.Write(fileContent);
        //close the file content block
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes("\r\n"));
        postWriter.Write(Encoding.GetEncoding(1252).GetBytes(CONTENT_BOUNDARY_PREFIX + CONTENT_BOUNDARY + "--"));
        
        //set the content length
        request.ContentLength = postStream.Length;
        request.Method = "POST";
        //get the stream for the request
        Stream requestStream = request.GetRequestStream();
        //write out our post data to the request stream
        postStream.WriteTo(requestStream);
        //get the response
        response = (HttpWebResponse)request.GetResponse();
        
        //get the response headers
        for (int i = 0; i < response.Headers.Count; i++)
        {
            output += response.Headers.Keys[i] + ": " + response.GetResponseHeader(response.Headers.Keys[i]) + "\r\n";
        }
        //get the encoding 
        Encoding enc = Encoding.GetEncoding(1252);
        
        try
        {
            if (response.ContentEncoding.Length > 0) enc = Encoding.GetEncoding(response.ContentEncoding);
        }
        catch {}
    
        // drag to a stream
        responseStream = new StreamReader(response.GetResponseStream(), enc); 
        output += "\r\n\r\n\r\n" + responseStream.ReadToEnd();
    }
    finally
    {
        if (postWriter != null) postWriter.Close();
        if (postStream != null)
        {
            postStream.Close();
            postStream.Dispose();
        }
        if (response != null) response.Close();
        if (responseStream != null)
        {
            responseStream.Close();
            responseStream.Dispose();
        }
    }
    return output;
}
Expand

See Also