i have problem in sending zip file in network.. all other formats i am able to send except .zip..
i tried a lot i dont no how to do this.. the code i have written at client side to upload file, this is suggested by microsoft here is the link
i am able to create zip file, if i try to open it says corrupted..size of the file also varying lot.
here is code
public void UploadFile(string localFile, string uploadUrl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
// Retrieve request stream and wrap in StreamWriter
Stream reqStream = req.GetRequestStream();
StreamWriter wrtr = new StreamWriter(reqStream);
// Open the local file
Stream Stream = File.Open(localFile, FileMode.Open);
// loop through the local file reading each line
// and writing to the request stream buffer
byte[] buff = new byte[1024];
int bytesRead;
while ((bytesRead = Stream.Read(buff, 0,1024)) > 0)
{
wrtr.Write(buff);
}
Stream.Close();
wrtr.Close();
try
{
req.GetResponse();
}
catch(Exception ee)
{
}
reqStream.Close();
please help me…
Thanks
The main problem is that you’re using a StreamWriter, which is a TextWriter, designed for text data, not binary files like zip files.
Additionally there’s the problem Jacob mentioned, and also the fact that you’re not closing the request stream until after you’ve received the response – although that won’t make any difference because the
StreamWriterwill close it first.Here’s the fixed code, changed to use
usingstatements (to avoid leaving streams open), a simpler call to theFileclass, and more meaningful names (IMO).Note that you can easily extract the while loop into a helper method:
to be used from other places, leaving just: