Hi folks,
Please help....
Im trying to download a file from webserver to my windows app using webrequest and webresponse classes. Its working fine on local machines and sometimes other machines. I colud not locate where is it going wrong. I made sure that IIS config is identical to my local machine.
FYI..... code snippets...
1. Download.aspx.cs
strGetFileList =
CType(Request.QueryString("GetFileList"), String)strFolderPath = ConfigurationManager.AppSettings.Get(
"DownloadPath") If (strGetFileList = "Yes") Then'If yes, first it will supply all the files available in the directory
strFileList = GetFileNamesList(strFolderPath)
Response.ClearHeaders()
'clearing the headers(if present) from the fileResponse.AddHeader(
"FileList", strFileList) 'put the file name in the addheader property of the "response" ElseIf (strGetFileList = "No") Then 'If NO, means it will ask for a particular file one by onestrFileName =
CType(Request.QueryString("FileName"), String)oFileStream =
New FileStream(strFolderPath & strFileName, FileMode.Open) 'clearing the headers(if present) from the fileResponse.ClearHeaders()
'put the file name in the addheader property of the "response"Response.AddHeader(
"FileName", strFileName) 'Response.ContentType = "application/octet-stream" ''copy the data to the output streamCopyData(oFileStream, Response.OutputStream)
Response.Write(Response.OutputStream)
End If Catch ex As ExceptionExceptionManager.Publish(ex)
FinallyResponse.OutputStream.Close()
If Not oFileStream Is Nothing Then oFileStream.Close() 'end the responseResponse.End()
End Try
2. Download.vb ( from my Winapp)
strQueryString =
"Download.aspx?GetFileList=Yes"strSourceURL = fstrSourceURL & strQueryString
Try 'Get the list of file names as , separated values in the web server to be downloaded 'then download each file by passing the file name ' This sets up the Request instance oRequest = WebRequest.Create(strSourceURL)oRequest.Timeout = 600000
' getting the default proxy settings for solving the bad data error during data sync oProxy = New WebProxy() oProxy = WebProxy.GetDefaultProxy If Not IsNothing(oProxy.Address) Then ' ' getting the default user id, password and domain name ' ' for user authentication. oProxy.Credentials = CredentialCache.DefaultCredentials oRequest.Proxy = oProxy End IfoRequest.Method =
"GET" ' Use a GET since no data is being sent to the web serveroResponse =
oRequest.GetResponse ' This causes the round-trip 'Get the file names of the file that is being downloaded as , separated valuesstrFileList = oResponse.Headers(
"FileList") If (strFileList.Length > 0) And (Not strFileList = Nothing) Then 'Remove the , from the endstrFileList = strFileList.Substring(0, (strFileList.Length - 1))
arrFileList = strFileList.Split(
",".ToCharArray()) Else Throw New Exception("No file found to download") End If'Loop through the files and downloadeach file from the destination folder For i = 0 To arrFileList.Length - 1
oRequest =
NothingoResponse =
NothingstrQueryString =
"Download.aspx?GetFileList=No"strSourceURL = fstrSourceURL & strQueryString &
"&FileName=" & arrFileList(i) ' This sets up the Request instanceoRequest =
CType(WebRequest.Create(strSourceURL), HttpWebRequest) ' Use a GET since no data is being sent to the web serveroRequest.Method =
"GET" ' This causes the round-tripoResponse =
oRequest.GetResponse 'Get the file name of the file that is being downloaded.strFileName = oResponse.Headers(
"FileName") 'check if the file name is empty,indicates that there is no file on Portal to download If Not strFileName = Nothing ThenstrDestURL = fstrDestURL & strFileName
'Check whether file exists, if exists delete the old fileoFileInfo =
New FileInfo(strDestURL) If (oFileInfo.Exists()) ThenoFileInfo.Delete()
End If ' Open the file to stream in the contentoFileStream =
New FileStream(strDestURL, FileMode.Create) ' Copy the content from the response stream to the file.CopyData(oResponse.GetResponseStream(), oFileStream)
m_strFileName = strFileName
If Not oResponse Is Nothing Then oResponse.GetResponseStream.Close() If Not oFileStream Is Nothing Then oFileStream.Close() Else Throw New Exception("File not found.") End If Next ibRetValue =
True Return bRetValue Catch ex As ExceptionExceptionManager.Publish(ex)
Throw ex Finally If Not oResponse Is Nothing Then oResponse.GetResponseStream.Close() If Not oFileStream Is Nothing Then oFileStream.Close()oRequest =
NothingoResponse =
NothingoFileInfo =
Nothing End Try--------------------------------------------------------------------------------------------------------------
should i need to configure anything, please suggest some way to work it out
Regards,
venkat
Replies
Know the answer? Post it — somebody with the same question will find it here.