AtTask API上传文件

时间:2014-06-21 01:08:50

标签: c# rest attask

我正在尝试将文件上传到AtTask,并且我继续收到非法的异常。我已阅读文档,并相信我有正确的代码,但我无法让它工作。有没有人有任何工作的C#代码上传文件到AtTask?以下是我的参考代码:

        BinaryReader br = new BinaryReader(fs);
        int bufferSize = 40960; //Amount of data that to be read from file(in bytes)
        byte[] buffer = br.ReadBytes(bufferSize);//reading the bytes
        string str = Convert.ToBase64String(buffer);

        string boundary = "----MyAppBoundary" + DateTime.Now.Ticks.ToString("x");

        var request = (HttpWebRequest)WebRequest.Create(fullUrl);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        request.Method = "POST";

        using (var requestStream = request.GetRequestStream())
        using (var writer = new StreamWriter(requestStream))
        {
            writer.WriteLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "uploadedFile"));
            writer.WriteLine();
            writer.WriteLine(str);                
        }

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

1 个答案:

答案 0 :(得分:0)

确保明确指定您的字符集(我一直使用UTF-8)。我从未通过AtTask API完成C#上传,但我有(生产系统功能)Java样本,如果这会有所帮助。这是我用来指定字符集的简短示例。

writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
      .append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=UTF-8")
      .append(LINE_FEED);