Thank you very much Mr. Hart.
But in my case it is throwing exception before executing that statement what
you have mentioned. That is before reading buffer size it is throwing
exception. As I have mentioned in my previous post exception occurs at the
line
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
And the code is as below. That is it is throwing exception even before
allocating buffer size.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Retrieve response stream
Stream respStream = resp.GetResponseStream();
// Create local file
FileStream wrtr = new FileStream(localFile, FileMode.Create);
// Allocate byte buffer to hold stream contents
byte[] inData = new byte[4096];
Still if you feel that buffer size is the cause for the issue then please
let me know how to know the size of the dll which will be on server and also
please explain how to code and apply that in my case ?
Thanks a lot.
Harsha.
:
That's probably because you are writing 4096 byte blocks. You're better off
declaring the size of the buffer for each write. So for example if your file
is 8,000 bytes, you need to subtract the 4096 from 8,000 and declare a buffer
in case of 3904 bytes. You're binary files currently will not have proper eof
markers. This is required for binary files not text files.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
:
Thank you Hart .
I am sending you the code I have used. Below is the function I have used to
download file to device. This is same as given in MSDN.
public void DownloadFileBinary(string localFile, string downloadUrl)
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(downloadUrl);
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Retrieve response stream
Stream respStream = resp.GetResponseStream();
// Create local file
FileStream wrtr = new FileStream(localFile, FileMode.Create);
// Allocate byte buffer to hold stream contents
byte[] inData = new byte[4096];
// loop through response stream reading each data block
// and writing to the local file
int bytesRead = respStream.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
wrtr.Write(inData, 0, bytesRead);
bytesRead = respStream.Read(inData, 0, inData.Length);
}
respStream.Close();
wrtr.Close();
}
I am calling this function as given below.
DownloadFileBinary("abc.dll",
"
http://16.138.51.50/DownloadCABForMypaq/abc.dll");
It is throwing exception at
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Error message is "An error message cannot be displayed because an optional
resource assembly containing it cannot be found"
And stack trace of the exception is
at System.Net.HttpWebRequest.finishGetResponse()
at System.Net.HttpWebRequest.GetResponse()
at UploadDownload.Form1.DownloadFileBinary()
at UploadDownload.Form1.btnDownloadbinary_Click()
at System.Windows.Forms.Control.OnClick()
at System.Windows.Forms.Button.OnClick()
at System.Windows.Forms.ButtonBase.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at UploadDownload.Program.Main()
Thank you very much for you Patience.
Harsha.
:
It should also work with binary files, show us the PDA code now you've
amended it.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
:
Thank you very much Mr. Hart for your time and response.
In MSDN I found out an article which is close to this. That says how to
upload and download files in compact framework.
Link of that document is
http://msdn2.microsoft.com/en-us/library/aa446517.aspx
I replicated the steps given ( please click on the above link ) and could
successfully download files to device. But I couldn't download any dll's. But
I can download text files,xml files,images.
I want to with this application can I download dll files ? If not what
changes I have to make do so ?
Thanks in advance.
Harsha.
:
On the client end you are using StreamReader and StreamWriter. These classes
are usally used for text files not binary plus you could have encoding
issues. Calling ReadLine and WriteLine will append \r\n carriage return and
line feed characters to each buffer read/write. This is probebly why it is
not working for you. Try using BinaryReader/BinaryWriter classes, and the
Read and Write methods.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
:
Thanks for below information. I tried a sample code. I am sharing same with
you.
sample code -> web application runnin on IIS
{
string strfilename =
"c:\\inetpub\\wwwroot\\DownloadCABForMypaq\\mypaq\\mypaq.exe";
System.IO.BinaryReader BR = new
System.IO.BinaryReader(File.Open(strfilename , FileMode.Open,
FileAccess.Read));
BR.BaseStream.Position = 0;
byte[] binFile = BR.ReadBytes(Convert.ToInt32(BR.BaseStream.Length));
Response.BinaryWrite(binFile);
BR.Close();
Response.End();
Response.Flush();
}
Client application - running in PDA (windows mobile 4x)
{
string FileName = "mypaq.exe";
string downloadUrl =
"
http://16.138.51.97/DownloadCABForMypaq/DownloadCAB.aspx?file=" + FileName;
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(downloadUrl);
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream respStream = resp.GetResponseStream();
StreamReader rdr = new StreamReader(respStream);
localFile = strAppExecPath1 + FileName;
// Create the local file
StreamWriter wrtr = new StreamWriter(localFile);
// loop through response stream reading each line
// and writing to the local file
string inLine = rdr.ReadLine();
int iSize = 0;
while (inLine != null)
{
wrtr.WriteLine(inLine);
iSize = iSize + inLine.Length;
inLine = rdr.ReadLine();
}
rdr.Close();
wrtr.Close();
}
Problem -> after download of the data from server it is stored in a file in
PDA.
when i try to run, the application does not run on PDA
the mypaq.exe is a device application compiled in .NET
can you check if i have done any mistake in coding.
Thanks for your patience.
Harsha.
:
Hi,
My scenario is I have set of Compact framework DLL's (Not winodws
assembly).
Till date I used to manually paste those into my device. While doing that
it
used to copy and convert those assemblies into device format.
Are you talking about the dialog that appears while transfering files to an
ActiveSync connected via Windows Explorer?
If so the conversion process mentioned in the dialog isn't doing anything
for .NET assemblies, i.e. it is simply passing them through unmodified.
Only files of certain file types are passed through a convertor when
transfered via ActiveSync and to my knowledge .NET assemblies are not one of
those file types. It's mainly wordprocessor and spreadsheet type formats
that are converted by ActiveSync for use with Pocket Word or Excel.
Now I want to automate that process and I don't want to manually copy them
rather I want to download from HTTP or HTTPS. So while downloading text
file
downloads properly. But for assembly's,images,videos format needs to be
changed as it was happening when we manually copy to device.