Problem using WriteFile with files stored on other servers?
I have an ASP.NET site in which I need to binary-write the contents of files to the users' browsers rather than linking to the files via the use of URLs. The following example shows how to open a file on a local drive using its physical path and then write the binary contents of the file to the browser:
<VBCODE>
Response.ContentType = "Application/pdf"
Response.WriteFile("C:\Test\Test.pdf")
Response.End()
</VBCODE>
The above code works fine, but if I put the file on a mapped drive (e.g. "G:\Test\Test.pdf"), I get an error which says "System.IO.DirectoryNotFoundException: Could not find a part of the path "G:\Test\Test.pdf".
If I use the full path to the file, which is on another server (e.g. "\\ServerName\cdrive\Test\Test.pdf"), I get the following error message: "System.IO.IOException: Logon failure: unknown user name or bad password"
I am able to access the file via Windows Explorer. I believe the problem may have something to do with authentication. Can anyone help me with this?
Thanks.
Re: Problem using WriteFile with files stored on other servers?
I am having the same problem...
Does nobody have an answer for this?
Re: Problem using WriteFile with files stored on other servers?
Show your code so that I can see what you're doing.
Re: Problem using WriteFile with files stored on other servers?
I'm just trying to copy a file from a network location to my web server directory:
VB Code:
FileCopy(strSourcePath, Server.MapPath(strFileName))
Re: Problem using WriteFile with files stored on other servers?
Either give the asp.net worker process permissions on that folder (Give Everyone permissions on that shared folder) or set impersonation on your application. Unless your security model does not go along those lines.
If that's not what you're looking for, I have seen another method to accomplish the same thing floating around, I can dig it up for you.
Re: Problem using WriteFile with files stored on other servers?
The problem is that the ASPNET user is not a domain user and so can't access the network. In addition, when deployed, the directory I need to access might not even be in the same domain.
I need some way of (ideally) logging onto the required domain, copying the file, and logging back out again.
Re: Problem using WriteFile with files stored on other servers?
Re: Problem using WriteFile with files stored on other servers?
Thanks...but my project is in VB and I don't know how to covert that code...:(
There's a link at the bottom of that page on an alternative method:
Accessing Shared Folders in ASP.NET
...But again it's in C
Re: Problem using WriteFile with files stored on other servers?
It's really really easy converting C# to VB.NET and vice versa!
I usually do it manually but you can try this:
http://www.developerfusion.co.uk/uti...sharptovb.aspx
Re: Problem using WriteFile with files stored on other servers?
Hmmm....That utility fails to convert either code snippets...:(
EDIT: Actually, I've realised that both those code samples contain unmanaged code...which if my memory serves me correctly, can't be implemented in VB.NET?
Re: Problem using WriteFile with files stored on other servers?
Unmanaged code can be implemented in VB.NET, as it has been in C#. What parts are you having trouble with? (Do it in parts)
Re: Problem using WriteFile with files stored on other servers?
Well, the code converter took issue with the key word Extern. Here is an example:
Code:
//WIN32API - WNetCancelConnection2
[DllImport("mpr.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetCancelConnection2A (
[MarshalAs (UnmanagedType.LPStr)]
string lpName,
int dwFlags,
int fForce
);