任务API上传文件
本文关键字:文件 API 任务 | 更新日期: 2023-09-27 18:09:32
我正在尝试上传文件到任务,但我继续收到非法参数异常。我已经阅读了文档,并相信我有正确的代码,但我不能让它工作。有人有任何工作的c#代码上传文件到任务吗?下面是我的代码供参考:
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);
}
}
确保明确指定了字符集(我一直使用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);