I have a HttpPostedFile object and after the file gets uploaded locally onto the server, i want to move that temp file in开发者_运维技巧to a document library in sharepoint. Here is my code:
private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses) {
for (int i = 0; i < context.Request.Files.Count; i++) {
HttpPostedFile file = context.Request.Files[i];
file.SaveAs(ingestPath + Path.GetFileName(file.FileName));
string fileName = Path.GetFileName(file.FileName);
}
Can anyone give me some example code for this? I have found a tutorial for Streams, but not quite sure if it would work the same in my situation
Replace the two lines starting with file.SaveAs
with the following:
var myDocumentLibrary = SPContext.Current.Web.Folders["MyDocumentLibrary"];
var myFile = myDocumentLibrary.Files.Add(file.Name, file.FileContent, true);
I have a code sample for you that comes in parts:
Here is code that gets the Files content into a byte array buffer:
var file = (HttpPostedFileBase)Request.Files[0];
var buffer = new byte[file.ContentLength];
file.InputStream.Read(buffer, 0, file.ContentLength);
var root = HttpContext.Current.Server.MapPath(@"~/_temp");
var temp_file_name = "somefilename";
var path = Path.Combine(root, temp_file_name);
using (var fs = new FileStream(path, FileMode.Create))
{
using (var br = new BinaryWriter(fs))
{
br.Write(buffer);
}
}
精彩评论