此picture介绍了我所做的事情。问题是,当我尝试在Sharepoint中打开文件时,它会引发一个popup。然后,当我单击“确定”时,它会给我this。单击恢复时,我可以看到内容。我该如何处理这些弹出窗口?
这是我的应用程序部分(TemplateModel包含我尝试发送到API的HttpPostedFileBase FileUpload属性):
[HttpPost]
public ActionResult CreateTemplate(TemplateModel template)
{
HttpResponseMessage result = new HttpResponseMessage();
if (TemplateModel.GetAll().Count != 0)
template.ID = TemplateModel.GetAll().Max(t => t.ID) + 1;
else
template.ID = 1;
#region posting to sharepoint
string URI = "http://localhost:50073/api/template/PostTemplate";
var item = template.FileUpload.InputStream.Length;
if (Request.Files.Count > 0)
{
using (HttpClient client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
byte[] fileBytes = new byte[template.FileUpload.InputStream.Length + 1];
template.FileUpload.InputStream.Read(fileBytes, 0, fileBytes.Length);
var fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentDisposition
= new ContentDispositionHeaderValue("attachment") { FileName = template.ID.ToString() };
fileContent.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
content.Add(fileContent);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("Content-Dsposition")
{
FileName = template.ID.ToString(),
};
result = client.PostAsync(URI, content).Result;
}
}
}
#endregion
if (result.StatusCode != HttpStatusCode.OK)
return RedirectToAction("Templates");
template.Create();
return RedirectToAction("Templates");
}
这是API部分。 API从Request.Content接收上传的文件:
[HttpPost]
public HttpResponseMessage PostTemplate()
{
var requestContent = Request.Content;
string id = Request.Content.Headers.ContentDisposition.FileName;
MemoryStream stream = new MemoryStream();
requestContent.CopyToAsync(stream);
byte[] data = stream.ToArray();
if (!_sharepoint.SubFolderExistsInFolder("/Documents/Templates"))
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Root of templates folder doesn't exist");
try
{
_sharepoint.PostFile(new SP.UploadProperties
{
Stream = stream,
FileName = string.Format("{0}.docx", id),
ServerURL = "/Documents/Templates/" + string.Format("{0}.docx", id) /*+ postedFile.FileName*/
}, data);
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.ToString());
}
}
如您所见,我将requestContent转换为MemoryStream以获得字节数组(byte []数据)。这是_sharepoint.PostMethod方法定义,用于将byte []作为CSOM文件上传到Sharepoint特定文件夹:
public void PostFile(UploadProperties properties, byte[] byteArray=null)
{
ClientContext ctx = GetContextObject();
List docLib = ctx.Web.Lists.GetByTitle("Documents");
ctx.Load(docLib);
ctx.ExecuteQuery();
FileCreationInformation createFile = new FileCreationInformation
{
Url = RelativeURL + properties.ServerURL,
Content = byteArray,
Overwrite = true
};
try
{
Microsoft.SharePoint.Client.File addedFile = docLib.RootFolder.Files.Add(createFile);
ctx.Load(addedFile);
ctx.ExecuteQuery();
docLib.Update();
}
catch (Exception e)
{
var test = e.ToString();
}
}