C# ASP.NET Open .Docx file that is located on the server on the client

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 19406 Views - Last Post: 20 August 2012 - 02:15 PM Rate Topic: ***** 1 Votes

#16 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 12:24 PM

View PostJstall, on 03 August 2012 - 02:23 PM, said:

Hi,

Sorry, I just want to clarify what you are attempting here. Are you trying to do some sort of manipulation of a docx file on the server then send it to the browser? Could you give us a better idea of what you are trying to do? What is happening when you run the code you have now? What does processReport() do?


Sorry for the delay I was out of town. What the program "should" do is, open a template word doc on the clients computer (the template is stored on the server), and once it is opened on the client the processReport() function replaces the specific variables in the docx with data from the program.

Everything works like a champ from the server, but if I goto the web application from a workstation sadly it says it cannot find the file (docx), but if I type the address in a browser it pulls up fine.

However I am not set in stone on the method, I would also be all for manipulating the file on the server an then sending it to the client, but I failed with that as well.

Thanks again everyone
Was This Post Helpful? 0
  • +
  • -

#17 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:24 PM

I'm not sure you'll be able to get this to work in the way that you want. It works on the server because it's executing from a trusted location. However once the file is loaded on the client the server mo longer has access to it. You can't modify files on a client computer from the web server. It would be a major security risk if you could.
Was This Post Helpful? 0
  • +
  • -

#18 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:26 PM

View PostNakor, on 06 August 2012 - 03:24 PM, said:

I'm not sure you'll be able to get this to work in the way that you want. It works on the server because it's executing from a trusted location. However once the file is loaded on the client the server mo longer has access to it. You can't modify files on a client computer from the web server. It would be a major security risk if you could.


Hmm how would you recommend I try to accomplish this? Could I some how load the file into a memory stream or something ?
Was This Post Helpful? 0
  • +
  • -

#19 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:26 PM

You would need to update the template before presenting it to the client
Was This Post Helpful? 0
  • +
  • -

#20 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:27 PM

View PostNakor, on 06 August 2012 - 03:26 PM, said:

You would need to update the template before presenting it to the client

I can't seem to even open the file on the client side
Was This Post Helpful? 0
  • +
  • -

#21 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:28 PM

Save the file as a new file and dynamically create a link to it
Was This Post Helpful? 0
  • +
  • -

#22 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:33 PM

you said that when you manually enter the path it works, by path do you mean the URL or the network path?
Was This Post Helpful? 0
  • +
  • -

#23 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 03:41 PM

View PostNakor, on 06 August 2012 - 03:33 PM, said:

you said that when you manually enter the path it works, by path do you mean the URL or the network path?

URL sorry
Was This Post Helpful? 0
  • +
  • -

#24 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 06 August 2012 - 06:13 PM

If manually entering the url works then the link that's being created must not be quite right. How are you creating the link to the file?
Was This Post Helpful? 0
  • +
  • -

#25 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 05:54 AM

Hi,


Are you simply trying to serve the file? Something like this: (untested)
//create a memory stream
using (MemoryStream stream = new MemoryStream())
{
	//read the file in 
	byte[] buffer = File.ReadAllBytes(Server.MapPath("pathtofile"));
	//write to the stream
	stream.Write(buffer, 0, buffer.Length);

	//set up response headers
	Response.ClearContent();
	Response.ClearHeaders();
	Response.AddHeader("content-disposition", "attachment; filename=document.docx");
	Response.ContentEncoding = System.Text.Encoding.UTF8;
	Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
	//write the stream to the response
	stream.WriteTo(Response.OutputStream);
	//end the response 
	Response.End();
}


Something like that? Again it's untested so there might be some errors in there

Edit, forgot code tags :-P

This post has been edited by Jstall: 07 August 2012 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#26 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 08:16 AM

View PostJstall, on 07 August 2012 - 05:54 AM, said:

Hi,


Are you simply trying to serve the file? Something like this: (untested)
//create a memory stream
using (MemoryStream stream = new MemoryStream())
{
	//read the file in 
	byte[] buffer = File.ReadAllBytes(Server.MapPath("pathtofile"));
	//write to the stream
	stream.Write(buffer, 0, buffer.Length);

	//set up response headers
	Response.ClearContent();
	Response.ClearHeaders();
	Response.AddHeader("content-disposition", "attachment; filename=document.docx");
	Response.ContentEncoding = System.Text.Encoding.UTF8;
	Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
	//write the stream to the response
	stream.WriteTo(Response.OutputStream);
	//end the response 
	Response.End();
}


Something like that? Again it's untested so there might be some errors in there

Edit, forgot code tags :-P


I need to Manipulate the file before serving it, but basically I will need to serve it at some point. If I had a way to load the file into a memory stream of sorts, and then modify the file prior to serving it then it would be perfect. I have attempted to do this with memory stream, but I didnt have any luck yet.
Was This Post Helpful? 0
  • +
  • -

#27 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 08:58 AM

Didn't you say you already had the file manipulation working? If so then just doing what I posted would send it to the browser as an attachment,
Was This Post Helpful? 0
  • +
  • -

#28 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 02:51 PM

View PostJstall, on 07 August 2012 - 08:58 AM, said:

Didn't you say you already had the file manipulation working? If so then just doing what I posted would send it to the browser as an attachment,


I have the manipulation working if I open the using the code that I posted, which opens an instance of word, and then manipulates the file.. I attempted to do everything client side with my above code, but it does not appear that is possible due to security risks.

I would be happy with server side manipulation, but I am just not seeing a way to make it work :(. The only problem I have with server side manipulation is I would need to create a temporary file, and then copy the template to that temp file (since I do not want the template to be modified), once the temp file was created I could manipulate the file.. but that is where I get stuck, from what I have read using the Microsoft.Interop.Word functions I can modify the contents of the file via an instance of the word application, but I do not see a way to do it on the server side, without opening up an instance of word (and sadly this has to be done with a DOCX file type).
Was This Post Helpful? 0
  • +
  • -

#29 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 05:35 PM

Hi,

I recommend you check out the OpenXML SDK just add the assembly to your project and you are good to go, no need to install MS Office on the server.

The SDK is pretty powerful, there are some resources out there on how to use it to manipulate Word documents, I suggest Googling around a bit for some examples.
Was This Post Helpful? 1
  • +
  • -

#30 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 07 August 2012 - 05:37 PM

View PostJstall, on 07 August 2012 - 05:35 PM, said:

Hi,

I recommend you check out the OpenXML SDK just add the assembly to your project and you are good to go, no need to install MS Office on the server.

The SDK is pretty powerful, there are some resources out there on how to use it to manipulate Word documents, I suggest Googling around a bit for some examples.


I will take a look at this and let you know if it resolves my issue, sounds like it will based on your explanation.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3