Skip to main content
Published: March 08 2010, 7:22:00 PMUpdated: November 28 2020, 8:16:22 PM

Sample code for uploadFile using XML in C#

The code sample below demonstrates how to make an uploadFile call using XML.

/*
© 2010-2013 eBay Inc., All Rights Reserved
Licensed under CDDL 1.0 - http://opensource.org/licenses/cddl1.php
*/

using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.IO.Compression;
.
.
.

private void uploadFile(string jobID, string InpFileRefID)
{
   string TaskRefID = jobID;
   string FileRefID = InpFileRefID;
   string endPoint = "
https://storage.ebay.com/FileTransferService";
    string boundary = "MIMEBoundaryurn_uuid_";

    string CRLF = "\r\n";
    string uuid = System.Guid.NewGuid().ToString();
    string reqUUID = System.Guid.NewGuid().ToString();
    string attchmtUUID = System.Guid.NewGuid().ToString();
    HttpWebResponse httpResp = null;

    //Create the HTTP request and set the headers
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(endPoint);

    httpRequest.Headers.Add("X-EBAY-SOA-SECURITY-TOKEN", YourAuthToken);
    httpRequest.Headers.Add("X-EBAY-SOA-SERVICE-NAME", "FileTransferService");
    httpRequest.Headers.Add("X-EBAY-SOA-SERVICE-VERSION", "1.0.0");                            
    httpRequest.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "uploadFile");

    httpRequest.ContentType = "multipart/related; boundary="
                    + boundary + uuid + ";type=\"application/xop+xml\";start=\"<0."
                    + "urn:uuid:" + reqUUID + ">\";start-info=\"text/xml\"";


    //Set the method to POST
    httpRequest.Method = "POST";

    //Read the gzip file as a byte array
    FileStream fs = File.OpenRead(<fileToUpload>);
    byte[] attachment = new byte[fs.Length];
    fs.Read(attachment, 0, attachment.Length);
    fs.Close();

    //Form the request string
    string uploadFileRequest = "--" + boundary + uuid + CRLF
                    + "Content-Type: application/xop+xml; charset=UTF-8; type=\"text/xml; charset=UTF-8\"" + CRLF
                    + "Content-Transfer-Encoding: binary" + CRLF
                    + "Content-ID: <0.urn:uuid:" + reqUUID + ">" + CRLF + CRLF

                    + "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                    + "<uploadFileRequest xmlns:sct=\"http://www.ebay.com/soaframework/common/types\" xmlns=\"http://www.ebay.com/marketplace/services\">" + "<taskReferenceId>" + TaskRefID + "</taskReferenceId>"
                    + "<fileReferenceId>" + FileRefID + "</fileReferenceId>"
                    + "<fileFormat>gzip</fileFormat>"
                    + "<fileAttachment>"
                    + "<Data><xop:Include xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" "
                    + "href=\"cid:urn:uuid:" + attchmtUUID + "\" /></Data>"
                    + "</fileAttachment>"
                    + "</uploadFileRequest>"
                    + CRLF + "--" + boundary + uuid + CRLF
                    + "Content-Type: application/octet-stream" + CRLF
                    + "Content-Transfer-Encoding: binary" + CRLF
                    + "Content-ID: <urn:uuid:" + attchmtUUID + ">" + CRLF + CRLF;

    string strReq2 = CRLF + "--" + boundary + uuid + "--" + CRLF;

    byte[] postDataBytes2 = System.Text.Encoding.UTF8.GetBytes(strReq2);

    //Convert the string to a byte array
    byte[] postDataBytes1 = System.Text.Encoding.UTF8.GetBytes(uploadFileRequest);

    int len = postDataBytes1.Length + postDataBytes2.Length + attachment.Length;
    httpRequest.ContentLength = len;

    //Post the request
    System.IO.Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(postDataBytes1, 0, postDataBytes1.Length);
    requestStream.Write(attachment, 0, attachment.Length);
    requestStream.Write(postDataBytes2, 0, postDataBytes2.Length);
    requestStream.Close(); 

    string response;

    // Get response and write to console
    httpResp = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader responseReader = new StreamReader(httpResp.GetResponseStream(), Encoding.UTF8);
    response = responseReader.ReadToEnd();
    httpResp.Close(); 

    Console.WriteLine(response);
}

*In order to execute an uploadFile call, you must first create a job via the createUploadJob method.

How well did this answer your question?
Answers others found helpful