We currently use the code below to download files from a network server to the client in an old ASP.net page.
We have re-written the app in MVC3 and would like to upgrade this functionality. I have seen several posts that claim you can access the file from the network share by writing the following lines in web.config
<authentication mode="Windows"/>
<identity impersonate="true" userName="" password="" />
However, we are currently using Forms authentication mode to logon to the site. Would that interfere with the login functionality?
Here’s our download code.
Partial Class DownloadFile2
Inherits System.Web.UI.Page
Private BufferSize As Integer = 32 * 1024
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String = Request.Params("File")
path = "\\speedy\wanfiles\" + path.Substring(3)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
Dim Buffer(BufferSize) As Byte
Dim SizeWritten, fileindex As Integer
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
Response.AddHeader("Content-Length", file.Length.ToString)
Response.ContentType = "application/octet-type"
Response.Flush()
fileindex = 0
Do
// not sure about this GM.AlasData code below
SizeWritten = GM.AlasData.ReadFileBlock(file.FullName, Buffer, fileindex, BufferSize)
Response.OutputStream.Write(Buffer, 0, SizeWritten)
Response.Flush()
If SizeWritten < BufferSize Then
Exit Do
Else
fileindex = fileindex + SizeWritten
End If
Loop
Response.End()
End Sub
End Class
I found this code to do downloads using MVC3 but cannot access the file because it is considered a local file.
public FileResult Download(string FilePath)
{
if (FilePath != null)
{
string path = FilePath;
string contentType;
// files are stored on network server named speedy
path = string.Concat(@"\\speedy\files\", HttpUtility.UrlDecode(path));
System.IO.FileInfo file = new System.IO.FileInfo(path);
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(file.Extension.ToLower());
if (rk != null && rk.GetValue("Content Type") != null)
{
contentType = rk.GetValue("Content Type").ToString();
}
else
{
contentType = "application/octet-type";
}
//Parameters to file are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(file.FullName, contentType, file.Name);
}
else
{
return null;
}
}
What needs to be done to fix this where we can perform this functionality?
http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C