Эта картинка представляет то, что я сделал. Проблема в том, что когда я пытаюсь открыть файл в Sharepoint, он вызывает popup . И затем, когда я нажимаю ОК, это дает мне это . Когда я нажимаю, чтобы восстановить, я могу видеть содержимое. Как я могу обработать эти всплывающие окна?
Вот моя часть приложения (TemplateModel содержит свойство HttpPostedFileBase FileUpload, которое я пытаюсь отправить в API):
[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 для загрузки байта [] в виде файла 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();
}
}