473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this FTP code?

Hi,

I must be missing something stupid. This works fine for text files,
but uploads about half of images ( jpg & png ) before cutting out, and
leaving a useless file on the server. It doesn't throw an exception,
though. My guess is the encoding is wrong, but I've tried UTF 8, and
use binary.

Any thoughts? This comes from the MSDN documentation:

try {
w = (FtpWebRequest) WebRequest.Crea te("ftp://" +
file.TargetPath );
w.Credentials = SiteConfigurati onList.Current. Credentials;
w.Method = WebRequestMetho ds.Ftp.UploadFi le;
w.UseBinary = true;

StreamReader sourceStream = new StreamReader(fi le.LocalPath,
true);
byte[] fileContents =
Encoding.UTF8.G etBytes(sourceS tream.ReadToEnd ());
sourceStream.Cl ose();
w.ContentLength = fileContents.Le ngth;

Stream requestStream = w.GetRequestStr eam();
requestStream.W rite(fileConten ts, 0, fileContents.Le ngth);
requestStream.C lose();

FtpWebResponse response = (FtpWebResponse )w.GetResponse( );
file.Status = file.TargetPath + "\n\n" + response.Status Code +
":\t" + response.Status Description + "\n" + response.ExitMe ssage;
frm.AddFtpStatu s(file);
response.Close( );
} catch (Exception ex) {
if (file != null)
file.ErrorStrin g = ex.Message;
if (frm != null)
frm.AddFtpStatu s(file);
}

Jan 6 '07 #1
8 2475
<Fo**********@g mail.comwrote:
I must be missing something stupid. This works fine for text files,
but uploads about half of images ( jpg & png ) before cutting out, and
leaving a useless file on the server. It doesn't throw an exception,
though. My guess is the encoding is wrong, but I've tried UTF 8, and
use binary.
You shouldn't use a StreamReader at all - you should use a Stream.

Readers are for text, and images aren't text.

Just open the file as a stream, then repeatedly read into a buffer,
write that buffer into the request stream, then read again until
reading returns no data.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '07 #2
Just open the file as a stream, then repeatedly read into a buffer,
write that buffer into the request stream, then read again until
reading returns no data.
Thanks! This works perfectly.

Jan 6 '07 #3
Hi,

You could also use a BinaryReader and BinaryWriter to simplify the process
if you'd like:

BinaryWriter writer = new BinaryWriter(ou tputStream);
BinaryReader reader = new BinaryReader(in putStream);

writer.Write(re ader.ReadBytes( (int) inputStream.Len gth));

--
Dave Sexton
http://davesexton.com/blog

<Fo**********@g mail.comwrote in message
news:11******** **************@ 42g2000cwt.goog legroups.com...
>Just open the file as a stream, then repeatedly read into a buffer,
write that buffer into the request stream, then read again until
reading returns no data.

Thanks! This works perfectly.

Jan 7 '07 #4
Hi,

Ignore my last post. BinaryReader and BinaryWriter are for text (I didn't
read your post carefully enough. Sorry :)

--
Dave Sexton
http://davesexton.com/blog

<Fo**********@g mail.comwrote in message
news:11******** **************@ 42g2000cwt.goog legroups.com...
>Just open the file as a stream, then repeatedly read into a buffer,
write that buffer into the request stream, then read again until
reading returns no data.

Thanks! This works perfectly.

Jan 7 '07 #5
Dave Sexton <dave@jwa[remove.this]online.comwrote :
Ignore my last post. BinaryReader and BinaryWriter are for text (I didn't
read your post carefully enough. Sorry :)
Actually they're not - BinaryReader and BinaryWriter are badly named
classes, really, and they *are* reasonably suitable, but:

1) Your suggestion relies on the input stream's length being known to
start with. It will be for a file, but when there's a simple solution
which doesn't require it, it's nice to be able to use *any* stream.

2) Reading the whole file into memory only to write it again is
unnecessarily wasteful. The request stream will have to remember the
whole thing anyway, so why create an extra copy of the lot? It's better
(IMO) to read it in chunks. That way for a 100MB file you'll only take
100MB + the buffer size, instead of 200MB. (Actually there'll probably
be some "spare space" in the request stream, but that's the case either
way.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '07 #6
Hi Jon,

You made some good points. I'll agree that it's a bit wasteful and in some
situations the somewhat less readable solution of using a Stream may be a
better approach. For large files, I completely agree.

As for the aptness of the BinaryReader and BinaryWriter, a constructor of
each takes an Encoding and the documentation states that the parameterless
constructors of each use UTF8 by default. How is it that these classes
aren't purely for text? If it's simply because it works with raw bytes then
what's the point of the encoding?

--
Dave Sexton
http://davesexton.com/blog

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Dave Sexton <dave@jwa[remove.this]online.comwrote :
>Ignore my last post. BinaryReader and BinaryWriter are for text (I
didn't
read your post carefully enough. Sorry :)

Actually they're not - BinaryReader and BinaryWriter are badly named
classes, really, and they *are* reasonably suitable, but:

1) Your suggestion relies on the input stream's length being known to
start with. It will be for a file, but when there's a simple solution
which doesn't require it, it's nice to be able to use *any* stream.

2) Reading the whole file into memory only to write it again is
unnecessarily wasteful. The request stream will have to remember the
whole thing anyway, so why create an extra copy of the lot? It's better
(IMO) to read it in chunks. That way for a 100MB file you'll only take
100MB + the buffer size, instead of 200MB. (Actually there'll probably
be some "spare space" in the request stream, but that's the case either
way.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 8 '07 #7
Dave Sexton <dave@jwa[remove.this]online.comwrote :
You made some good points. I'll agree that it's a bit wasteful and in some
situations the somewhat less readable solution of using a Stream may be a
better approach. For large files, I completely agree.
I try to use a consistent approach to stream handling throughout - it
means I only need to remember one way of doing things :)

(I'd also have a CopyStream method to do the copying using a buffer, so
it would be pretty obvious what's going on.)
As for the aptness of the BinaryReader and BinaryWriter, a constructor of
each takes an Encoding and the documentation states that the parameterless
constructors of each use UTF8 by default. How is it that these classes
aren't purely for text? If it's simply because it works with raw bytes then
what's the point of the encoding?
It's for text *and* binary data. The BinaryReader.Re adString and
BinaryWriter.Wr ite(string) methods use the encoding, and likewise
ReadChars and BinaryWriter.Wr ite(char[], int, int) methods, but the
methods which only deal with bytes and primitive types don't care about
encodings.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 8 '07 #8
Hi Jon,

That's good to know, thanks :)

--
Dave Sexton
http://davesexton.com/blog

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Dave Sexton <dave@jwa[remove.this]online.comwrote :
>You made some good points. I'll agree that it's a bit wasteful and in
some
situations the somewhat less readable solution of using a Stream may be a
better approach. For large files, I completely agree.

I try to use a consistent approach to stream handling throughout - it
means I only need to remember one way of doing things :)

(I'd also have a CopyStream method to do the copying using a buffer, so
it would be pretty obvious what's going on.)
>As for the aptness of the BinaryReader and BinaryWriter, a constructor of
each takes an Encoding and the documentation states that the
parameterles s
constructors of each use UTF8 by default. How is it that these classes
aren't purely for text? If it's simply because it works with raw bytes
then
what's the point of the encoding?

It's for text *and* binary data. The BinaryReader.Re adString and
BinaryWriter.Wr ite(string) methods use the encoding, and likewise
ReadChars and BinaryWriter.Wr ite(char[], int, int) methods, but the
methods which only deal with bytes and primitive types don't care about
encodings.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 8 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14640
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
72
5797
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to...
121
9988
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather...
51
13335
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
46
4184
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5029
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
1460
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay http://www.ebay.com google http://www.google.com yahoo http://www.yahoo.com However in the drop down list displayed value used ebay ...
98
4545
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
2114
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
2801
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.