I am trying to copy a file from a shared folder to folder in IIS.
Following is my code
Imports System.Net
Imports System.IO
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Dim uploadUrl, fileName As String
Dim term As Integer = 1
Dim storenum As String = 2234
Dim sourcefile As String = e.CommandArgument.ToString()
Dim slashPosition As Integer = sourcefile.LastIndexOf("\")
Dim filenameOnly As String = sourcefile.Substring(slashPosition + 1)
Dim destfile As String = Server.MapPath("~/UploadFiles/") & filenameOnly
'nicklibee-pc is the name of my pc
'prj1 is the name of the project currently working in iis.
'uFiles is the name of the folder in IIS to upload the file
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = FileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
' The request stream must be closed before getting the response.
requestStream.Close()
uploadResponse = uploadRequest.GetResponse()
Console.WriteLine("Upload complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Finally
If uploadResponse IsNot Nothing Then
uploadResponse.Close()
End If
If FileStream IsNot Nothing Then
FileStream.Close()
End If
If requestStream IsNot Nothing Then
requestStream.Close()
End If
End Try
Unable to connect to the remote server is the error message iam getting
error happens in requestStream = uploadRequest.GetRequestStream()
Does the IIS user under which your application runs have access to the FTP site ? Check the permissions. It seems to be a permission issue. If you have a special user who has been granted permission to the FTP site then you can provide the credentials of
that user to your WebRequest or execute your application under that user's credential in IIS.
Providing credentials to the WebRequest
uploadRequest.Credentials = new NetworkCredentials("username", "password");
No, user will not upload any file from his local machine.
When user access the application, application should automaticalyl copy the files from a shared network drive to IIS.
This is what i am doing
Following is my modified code.
Please help me
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Dim uploadUrl, fileName As String
uploadUrl = "ftp://nick_pc/sys-app/sys-app/TransferFiles/aaa.xls"
fileName = drv
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(uploadUrl), System.Net.FtpWebRequest)
'Here serverpath where we have to upload.
'clsRequest.Credentials = New System.Net.NetworkCredential("", "")
'username and password
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim bFile As Byte() = System.IO.File.ReadAllBytes(sourcefile)
'Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
In that case no need to use FTP. Use the Directory and File classes of System.IO namespace and copy all the required files. You will have to provide required permissions to the IIS user to access these folders. Best way is to provide Full Access to both
Source and Destination folder to the IIS user under which the application is running.
If you are running your .Net application under default user, better to create a specific user and that user while copying the files. This specific user should be given permissions to access source and destination folders.
E.g.
string[] files = Directories.GetFiles;
foreach(file in files){File.Copy(file, destination);}
nicklibee
Member
650 Points
682 Posts
copy a file from a shared folder to iis - error
Dec 26, 2011 04:40 AM|LINK
Hello all,
I am trying to copy a file from a shared folder to folder in IIS.
Following is my code
Imports System.Net
Imports System.IO
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Dim uploadUrl, fileName As String
Dim term As Integer = 1
Dim storenum As String = 2234
Dim sourcefile As String = e.CommandArgument.ToString()
Dim slashPosition As Integer = sourcefile.LastIndexOf("\")
Dim filenameOnly As String = sourcefile.Substring(slashPosition + 1)
Dim destfile As String = Server.MapPath("~/UploadFiles/") & filenameOnly
uploadUrl = "ftp://nicklibee-pc/proj1/proj1/UFiles/"
fileName = sourcefile
'nicklibee-pc is the name of my pc
'prj1 is the name of the project currently working in iis.
'uFiles is the name of the folder in IIS to upload the file
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing
requestStream = uploadRequest.GetRequestStream()
FileStream = File.Open(fileName, FileMode.Open)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = FileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
' The request stream must be closed before getting the response.
requestStream.Close()
uploadResponse = uploadRequest.GetResponse()
Console.WriteLine("Upload complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Finally
If uploadResponse IsNot Nothing Then
uploadResponse.Close()
End If
If FileStream IsNot Nothing Then
FileStream.Close()
End If
If requestStream IsNot Nothing Then
requestStream.Close()
End If
End Try
Unable to connect to the remote server is the error message iam getting
error happens in requestStream = uploadRequest.GetRequestStream()
kindly help me to rectify this problem
nick
setahamid
Participant
1299 Points
245 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 07:44 AM|LINK
check following it may help you
http://forums.asp.net/t/1450307.aspx/1
Hamid Seta
http://www.hamidseta.blogspot.com/
Remember to click “Mark as Answer” on the post, if it helps you.
nicklibee
Member
650 Points
682 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 08:41 AM|LINK
I AM NOT USING FILE UPLOAD CONTROL TO UPLOAD THE FILE
THNKS
NICK
bhavay11
Member
66 Points
31 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 10:37 AM|LINK
Does the IIS user under which your application runs have access to the FTP site ? Check the permissions. It seems to be a permission issue. If you have a special user who has been granted permission to the FTP site then you can provide the credentials of that user to your WebRequest or execute your application under that user's credential in IIS.
Providing credentials to the WebRequest
uploadRequest.Credentials = new NetworkCredentials("username", "password");
Please mark as answer if this helped you.
nicklibee
Member
650 Points
682 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 11:03 AM|LINK
I am in the development PC so you mean i should provide permissions to the local IIS ?
Do i need to provide the permission to the IIS in server also after moving it into the production?
thanks
nick
nicklibee
Member
650 Points
682 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 11:09 AM|LINK
The scenario is like this...
I have a shared folder and this folder has some documents.
I wanted to transfer the documents to the virtual directory of my project in IIS.
For this iam using the code pasted above.
Do you know any other mechanism to copy the files from a shared folder to a virtual directory in IIS?
THANKS
Nick
bhavay11
Member
66 Points
31 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 11:11 AM|LINK
Yes.
You also need to do this in Production.
Can you tell the exact functionality you are trying to implement. Is the user going to upload a file from his local machine to the server ?
nicklibee
Member
650 Points
682 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 11:26 AM|LINK
No, user will not upload any file from his local machine.
When user access the application, application should automaticalyl copy the files from a shared network drive to IIS.
This is what i am doing
Following is my modified code.
Please help me
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Dim uploadUrl, fileName As String
uploadUrl = "ftp://nick_pc/sys-app/sys-app/TransferFiles/aaa.xls"
fileName = drv
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(uploadUrl), System.Net.FtpWebRequest)
'Here serverpath where we have to upload.
'clsRequest.Credentials = New System.Net.NetworkCredential("", "")
'username and password
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim bFile As Byte() = System.IO.File.ReadAllBytes(sourcefile)
'Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
thanks
nick
bhavay11
Member
66 Points
31 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 11:53 AM|LINK
In that case no need to use FTP. Use the Directory and File classes of System.IO namespace and copy all the required files. You will have to provide required permissions to the IIS user to access these folders. Best way is to provide Full Access to both Source and Destination folder to the IIS user under which the application is running.
If you are running your .Net application under default user, better to create a specific user and that user while copying the files. This specific user should be given permissions to access source and destination folders.
E.g.
string[] files = Directories.GetFiles;
foreach(file in files){File.Copy(file, destination);}
In your web.config
<Identity impersonate=true username=user_for_file_copy password=password />
You can optionally do this in IIS.
This user_for_file_copy should have permissions on the source and destination folder.
If your authentication mode is Windows then you are not required to give any permissions while on local.
nicklibee
Member
650 Points
682 Posts
Re: copy a file from a shared folder to iis - error
Dec 26, 2011 12:11 PM|LINK
Ok thanks i did it according to your suggestions and it works fine in my liocall machine but when i moved the same to server its not working
what could be the reason, i made the "ASP.Net Impersonation" Enabled and "Windows Authentication" Enabled in IIS
Following is my code
Dim sourcefile As String = e.CommandArgument.ToString()
Dim sourcefilelen As Integer = Len(sourcefile)
Dim corpath As String = Mid(sourcefile, 3, sourcefilelen)
Dim drivefinder As String = Left(e.CommandArgument.ToString(), 2)
Dim drv As String
If drivefinder = "F:" Then
drv = "\\nick-srvr\documents"
End If
drv = drv & corpath
Dim term As Integer = 1
Dim storenum As String = 2234
Dim slashPosition As Integer = sourcefile.LastIndexOf("\")
Dim filenameOnly As String = sourcefile.Substring(slashPosition + 1)
Dim destfile As String = "\\srvr-web1\sys.appli\ sys.appli \FilesUpload\" & filenameOnly
System.IO.File.Copy(drv, destfile)
please help me
nick