问题
I'm coding using visual studio, C#. Like the above mentioned, is there any ways i can open a word file stored in Sharepoint's document library? Just have to open the word file for me to read. Do not have to edit or delete.
I have tried: System.Diagnostics.Process.Start("iexplore.exe",url) but it doesn't seems to work.
Tried the following codes. No problem displaying my label. But the word doc wouldn't launch as well.
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
this.Label1.Text = "You selected " + row.Cells[4].Text + ".";
System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\Test\Test.docx");
}
}
回答1:
This could potentially be a lot harder than it sounds. If you're trying to do this from the server-side object model, then you'll most likely be running in session 0. Session 0 is a special Windows session for services, which doesn't permit the spawning of new processes. To get around this, you'd have to get out of session 0 by using the Windows API to log a user in and then take control of their session. I'll go no further down this path, since you've not specified that you're actually doing this.
If you're just looking to download a word document stored in a document library on SharePoint and then start it on the client side, you can use the following to download and open the document:
ClientContext clientContext = new ClientContext("http://SharePoint/");
string serverRelativeUrlOfFile = "SharedDocuments/file.docx";
string fileDestinationPath = @"C:\Documents\file.docx";
using (FileInformation sharePointFile =
Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverRelativeUrlOfFile))
{
using (Stream destFile = System.IO.File.OpenWrite(fileDestinationPath))
{
byte[] buffer = new byte[8 * 1024];
int byteReadInLastRead;
while ((byteReadInLastRead = sharePointFile.Stream.Read(buffer, 0, buffer.Length)) > 0)
{
destFile.Write(buffer, 0, byteReadInLastRead);
}
}
}
System.Diagnostics.Process.Start(@"C:\Documents\file.docx");
EDIT: Note, this uses the SharePoint2010 Client object model, as the steps for the server-side object model are made considerably harder by session 0 isolation, and I won't detail that here unless it's requested.
EDIT: After the comments, it's clear that you're trying to use a Webpart to download and execute a file on the client side. The above code is from the SharePoint 2010 Client Object Model, but the C# version. Translating it to the JavaScript version should be trivial now that I've shown you the correct C# version. This will download the file to the client. Once the file is downloaded, execute the file using the following JavaScript/ActiveX:
var objShell = new ActiveXObject("Shell.Application");
var strArguments = "";
var strDirectory = "";
var strOperation = "open";
var intShow = 1;
objShell.ShellExecute(FileLocation, strArguments, strDirectory, strOperation, intShow);
Where the correct parameters are substituted. Note that this will only work on Internet Explorer -- as this obviously hilariously unsafe piece of ActiveX is blocked by every half-decent browser out there -- and I've only tested it on IE8.
来源:https://stackoverflow.com/questions/15601759/sharepoint-using-c-sharp-to-open-a-word-doc-file-stored-in-sharepoint-document