File upload AtTask/Workfront API文件上载

File upload AtTask/Workfront API文件上载,file-upload,attask,workfront-api,File Upload,Attask,Workfront Api,我正在编写一个通用的上传,这样我就可以通过API上传项目,但没有太大的成功。收到500个未经授权的请求或400个错误请求。这是我在谷歌搜索后的尝试。不确定这是否过于复杂。他们还尝试了一种精简的方法。有几个帖子,但没有一个真正有效或被确认有效。稍后我会发布一个简化版本,我知道这不会返回值。我只是想在优化之前通过异常 public JToken DoUpload(string path, string file, params string[] parameters) { s

我正在编写一个通用的上传,这样我就可以通过API上传项目,但没有太大的成功。收到500个未经授权的请求或400个错误请求。这是我在谷歌搜索后的尝试。不确定这是否过于复杂。他们还尝试了一种精简的方法。有几个帖子,但没有一个真正有效或被确认有效。稍后我会发布一个简化版本,我知道这不会返回值。我只是想在优化之前通过异常

    public JToken DoUpload(string path, string file, params string[] parameters)
    {
    string result = string.Empty;

    if (!path.StartsWith("/"))
        {
            path = "/" + path;
        }

        string fullUrl = url + path;// +ToQueryString(parameters);

        string contentType = "multipart/form-data";
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        //wr.Headers.Add("sessionID","XXXXXXXXXXXXXX");  NEEDED?

        Stream rs = wr.GetRequestStream();

        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string formitem = string.Format(formdataTemplate, "uploadedFile", "temp.jpg", contentType);
        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
        rs.Write(formitembytes, 0, formitembytes.Length);
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string header = string.Format(headerTemplate, "uploadedFile", "temp.jpg", contentType);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length);

        FileStream fileStream = new FileStream(file, FileMode.Open,                 FileAccess.Read);
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            rs.Write(buffer, 0, bytesRead);
        }
        fileStream.Close();

        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        rs.Close();

        WebResponse wresp = null;
        Stream stream2;
        StreamReader reader2;
        try
        {
            wresp = wr.GetResponse();
            stream2 = wresp.GetResponseStream();
            reader2 = new StreamReader(stream2);
        }
        catch (Exception ex)
        {
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }

    return result;
    }

找到了一个更简单的解决方案,如果对任何人都有帮助:

public JToken DoUpload(string path, string file, string sessionID){

        if (!path.StartsWith("/")){
            path = "/" + path;
        }

        string fullUrl = url + path;
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(fullUrl);
        webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
        webrequest.Method = "POST";
        webrequest.Accept = "application/json;charset=UTF-8";
        webrequest.Headers.Add("sessionID", sessionID);

        // Build up the post message header
        StringBuilder sb = new StringBuilder();
        sb.Append("--");
        sb.Append(boundary);
        sb.Append("\r\n");
        sb.Append("Content-Disposition: form-data; name=\"");
        sb.Append("uploadedFile");
        sb.Append("\"; filename=\"");
        sb.Append(Path.GetFileName(file));
        sb.Append("\"");
        sb.Append("\r\n");
        sb.Append("Content-Type: ");
        sb.Append("application/octet-stream");
        sb.Append("\r\n");
        sb.Append("\r\n");

        string postHeader = sb.ToString();
        byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

        // Build the trailing boundary string as a byte array
        // ensuring the boundary appears on a line by itself
        byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

        FileStream fileStream = new FileStream(file,FileMode.Open, FileAccess.Read);
        long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
        webrequest.ContentLength = length;

        Stream requestStream = webrequest.GetRequestStream();

        // Write out our post header
        requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

        // Write out the file contents
        byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            requestStream.Write(buffer, 0, bytesRead);
        }

        // Write out the trailing boundary
        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

        using (WebResponse response = webrequest.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                return ReadResponse(responseStream);
            }
        }     
    }