1

I need to upload an image to a server, less then 20 lines of code please. and no questions about services. My boss expects it to work like ftp of old days...

Lyle
  • 419
  • 1
  • 6
  • 26

1 Answers1

1

I don't think there is a 20 lines solution to your problem.

But check this link out :

http://code.msdn.microsoft.com/Windows-8-SocketsFtp-4fc23b33#content

It contains a full Ftp client that works on Windows 8.1 metro application.

You could use this project as a library in your own project.

If you especially need to upload file, I think you need to use the UploadFileAsync function in FtpClient class

public async Task UploadFileAsync(StorageFile file, string destination) 
    { 
        using (var stream = await OpenWriteAsync(destination)) 
        { 
            // 
            // A more efficient way, maybe a DataReader can be used here 
            using (var readStream = await file.OpenReadAsync()) 
            { 
                var buffer = new byte[512].AsBuffer(); 
                var resultingBuffer = new byte[0]; 

                while (true) 
                { 
                    IBuffer readBuffer = await readStream.ReadAsync(buffer, 512, InputStreamOptions.Partial); 

                    if (readBuffer.Length == 0) break; 

                    resultingBuffer = resultingBuffer.Concat(readBuffer.ToArray()).ToArray(); 
                } 

                await stream.WriteAsync(resultingBuffer.AsBuffer()); 
                await stream.FlushAsync(); 
            } 
        } 
    }  
Aurelien Souchet
  • 721
  • 5
  • 11