我在ASP MVC Web应用程序中有一个控制器,试图将文档上传到Opentext eDocs REST API。
这是我的方法
var uri = ConfigurationManager.AppSettings["eDocsEndpoint"] + "documents?library=EDOCS_TEST";
var cookieContainer = GetCookies(ConfigurationManager.AppSettings["eDocsUser"], ConfigurationManager.AppSettings["eDocsPassword"], ConfigurationManager.AppSettings["eDocsLibrary"]);
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
{
using (var client = new HttpClient(handler) { BaseAddress = new Uri(uri) })
{
FileStream fileStream;
StreamContent streamContent;
using (fileStream = System.IO.File.OpenRead("C:\\Temp\\Test.docx"))
{
var memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
streamContent = new StreamContent(memStream);
}
using (var form = new MultipartFormDataContent())
{
var values = new[]
{
new KeyValuePair<string, string>("DOCNAME", "DOCNAME"),
new KeyValuePair<string, string>("AUTHOR_ID", "MYUSER"),
new KeyValuePair<string, string>("CLIENT_ID", "DEFAULT"),
new KeyValuePair<string, string>("MATTER_ID", "DEFAULT"),
new KeyValuePair<string, string>("TYPIST_ID", "MYUSER"),
new KeyValuePair<string, string>("APP_ID", "MS WORD"),
new KeyValuePair<string, string>("TYPE_ID", "DEFAULT")
};
foreach (var keyValuePair in values)
{
form.Add(new StringContent(keyValuePair.Value,Encoding.UTF8),
String.Format("\"{0}\"", keyValuePair.Key));
}
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
form.Add(streamContent, "\"file\"", string.Format("\"{0}\"", "TEST"));
try
{
var response = client.PostAsync("documents?library=" + ConfigurationManager.AppSettings["eDocsLibrary"], form).Result;
if (!response.IsSuccessStatusCode)
{
//TODO
}
return Json("Success", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json("Failed", JsonRequestBehavior.AllowGet);
}
}
}
}
我还在运行REST API的服务器中调试。这是错误:
Traceback (most recent call last):
File "C:\Python27\lib\logging\__init__.py", line 861, in emit
msg = self.format(record)
File "C:\Python27\lib\logging\__init__.py", line 734, in format
return fmt.format(record)
File "C:\Python27\lib\logging\__init__.py", line 465, in format
record.message = record.getMessage()
File "C:\Python27\lib\logging\__init__.py", line 329, in getMessage
msg = msg % self.args
File "build\bdist.win-amd64\egg\webob\request.py", line 1165, in as_text
return bytes.decode(self.charset)
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd2 in position 1392: invalid continuation byte
Logged from file log.py, line 94
我可以使用python脚本上载文档,但是我需要在此ASP NET C#Web应用程序中使用它。