Windows Phone 8 FtpClient 不会上传以前用于 BitmapImage 问题的流数据
Windows Phone 8 FtpClient won't upload stream data previously used for issue with BitmapImage
我会尽力把问题解释清楚。我正在使用
中的代码
http://msdn.microsoft.com/en-us/magazine/dn385710.aspx
创建照片共享应用程序。我可以 FTP 图片到我的服务器正常。我面临的问题是当我尝试创建应用程序来预览图片然后 FTP 它时。 FTP 仍会传输文件,但文件大小始终为 0 大小。我不确定这是一个错误还是我在 FTP 之前没有处理某个对象。以下是我的代码:
第 1 页
BitmapImage bitmapImage;
public PhotoPreview()
{
InitializeComponent();
btnYes.Tap += btnYes_Tap;
imgPreview.Width = G.getScreenWidth();
imgPreview.Height = G.getScreenHeight() - 250;
//windows phone 8 bug. When bitmap image is set...it blocks the FTP process thread. Will perform FTP on seperate page
previewPhoto();
}
void previewPhoto()
{
bitmapImage = new BitmapImage();
bitmapImage.SetSource(G.myStream);
imgPreview.Source = bitmapImage;
}
private void btnYes_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
disposeImage(bitmapImage);
NavigationService.Navigate(new Uri("/PhotoFTP.xaml", UriKind.Relative));
}
private void disposeImage(BitmapImage img)
{
if (img != null)
{
try
{
using (var ms = new MemoryStream(new byte[] { 0x0 }))
{
img = new BitmapImage();
img.SetSource(ms);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("ImageDispose FAILED " + e.Message);
}
}
}
第 2 页
const string
IP_ADDRESS = "888.88.888",
FTP_USERNAME = "test",
FTP_PASSWORD = "test123"
;
string filename;
FtpClient ftpClient = null;
TestLogger logger = null;
public PhotoFTP()
{
InitializeComponent();
DateTime thisDay = DateTime.Today;
string timestamp = thisDay.Hour.ToString() + "_" + thisDay.Minute.ToString() + "_" + thisDay.Second.ToString();
filename = timestamp + ".jpg";
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Test_connect();
}
private async void Test_connect()
{
logger = TestLogger.GetDefault(this.Dispatcher);
lstLogs.ItemsSource = logger.Logs;
ftpClient = new FtpClient(IP_ADDRESS, this.Dispatcher);
ftpClient.FtpConnected += ftpClient_FtpConnected;
ftpClient.FtpFileUploadSucceeded += ftpClient_FtpFileUploadSucceeded;
ftpClient.FtpFileUploadFailed += ftpClient_FtpFileUploadFailed;
ftpClient.FtpAuthenticationSucceeded += ftpClient_FtpAuthenticationSucceeded;
ftpClient.FtpAuthenticationFailed += ftpClient_FtpAuthenticationFailed;
logger = TestLogger.GetDefault(this.Dispatcher);
await ftpClient.ConnectAsync();
logger.AddLog("Connecting...");
}
async void ftpClient_FtpConnected(object sender, EventArgs e)
{
logger.AddLog("Preparing...");
await (sender as FtpClient).AuthenticateAsync(FTP_USERNAME, FTP_PASSWORD);
}
private async void Test_upload()
{
logger.AddLog("Uploading photo...");
await ftpClient.UploadFileAsync(G.myStream, "username_timestamp.jpg");
}
void ftpClient_FtpAuthenticationFailed(object sender, EventArgs e)
{
logger.AddLog("Connection error.");
}
void ftpClient_FtpAuthenticationSucceeded(object sender, EventArgs e)
{
logger.AddLog("Connection established.");
Test_upload();
}
void ftpClient_FtpFileUploadFailed(object sender, FtpFileTransferFailedEventArgs e)
{
logger.AddLog("Failed.");
}
Boolean firstTime = true;
void ftpClient_FtpFileUploadSucceeded(object sender, FtpFileTransferEventArgs e)
{
logger.AddLog("Completed.");
}
如果我注释掉第 1 页中的 previewPhoto();
行,它会 FTP 将文件正常发送到我的服务器。我认为问题是
bitmapImage.SetSource(G.myStream);
我也试过创建两个独立的流。一个用于预览照片,另一个用于 FTP。当 FTP 到我的服务器时,结果仍然导致文件大小为 0。
因为BitmapImage
读到流的末尾,所以FtpClient
没有数据到read/upload。
使用Stream.Seek
将流指针重置回开头。
G.myStream.Seek(0, SeekOrigin.Begin);
我会尽力把问题解释清楚。我正在使用
中的代码http://msdn.microsoft.com/en-us/magazine/dn385710.aspx
创建照片共享应用程序。我可以 FTP 图片到我的服务器正常。我面临的问题是当我尝试创建应用程序来预览图片然后 FTP 它时。 FTP 仍会传输文件,但文件大小始终为 0 大小。我不确定这是一个错误还是我在 FTP 之前没有处理某个对象。以下是我的代码:
第 1 页
BitmapImage bitmapImage;
public PhotoPreview()
{
InitializeComponent();
btnYes.Tap += btnYes_Tap;
imgPreview.Width = G.getScreenWidth();
imgPreview.Height = G.getScreenHeight() - 250;
//windows phone 8 bug. When bitmap image is set...it blocks the FTP process thread. Will perform FTP on seperate page
previewPhoto();
}
void previewPhoto()
{
bitmapImage = new BitmapImage();
bitmapImage.SetSource(G.myStream);
imgPreview.Source = bitmapImage;
}
private void btnYes_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
disposeImage(bitmapImage);
NavigationService.Navigate(new Uri("/PhotoFTP.xaml", UriKind.Relative));
}
private void disposeImage(BitmapImage img)
{
if (img != null)
{
try
{
using (var ms = new MemoryStream(new byte[] { 0x0 }))
{
img = new BitmapImage();
img.SetSource(ms);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("ImageDispose FAILED " + e.Message);
}
}
}
第 2 页
const string
IP_ADDRESS = "888.88.888",
FTP_USERNAME = "test",
FTP_PASSWORD = "test123"
;
string filename;
FtpClient ftpClient = null;
TestLogger logger = null;
public PhotoFTP()
{
InitializeComponent();
DateTime thisDay = DateTime.Today;
string timestamp = thisDay.Hour.ToString() + "_" + thisDay.Minute.ToString() + "_" + thisDay.Second.ToString();
filename = timestamp + ".jpg";
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Test_connect();
}
private async void Test_connect()
{
logger = TestLogger.GetDefault(this.Dispatcher);
lstLogs.ItemsSource = logger.Logs;
ftpClient = new FtpClient(IP_ADDRESS, this.Dispatcher);
ftpClient.FtpConnected += ftpClient_FtpConnected;
ftpClient.FtpFileUploadSucceeded += ftpClient_FtpFileUploadSucceeded;
ftpClient.FtpFileUploadFailed += ftpClient_FtpFileUploadFailed;
ftpClient.FtpAuthenticationSucceeded += ftpClient_FtpAuthenticationSucceeded;
ftpClient.FtpAuthenticationFailed += ftpClient_FtpAuthenticationFailed;
logger = TestLogger.GetDefault(this.Dispatcher);
await ftpClient.ConnectAsync();
logger.AddLog("Connecting...");
}
async void ftpClient_FtpConnected(object sender, EventArgs e)
{
logger.AddLog("Preparing...");
await (sender as FtpClient).AuthenticateAsync(FTP_USERNAME, FTP_PASSWORD);
}
private async void Test_upload()
{
logger.AddLog("Uploading photo...");
await ftpClient.UploadFileAsync(G.myStream, "username_timestamp.jpg");
}
void ftpClient_FtpAuthenticationFailed(object sender, EventArgs e)
{
logger.AddLog("Connection error.");
}
void ftpClient_FtpAuthenticationSucceeded(object sender, EventArgs e)
{
logger.AddLog("Connection established.");
Test_upload();
}
void ftpClient_FtpFileUploadFailed(object sender, FtpFileTransferFailedEventArgs e)
{
logger.AddLog("Failed.");
}
Boolean firstTime = true;
void ftpClient_FtpFileUploadSucceeded(object sender, FtpFileTransferEventArgs e)
{
logger.AddLog("Completed.");
}
如果我注释掉第 1 页中的 previewPhoto();
行,它会 FTP 将文件正常发送到我的服务器。我认为问题是
bitmapImage.SetSource(G.myStream);
我也试过创建两个独立的流。一个用于预览照片,另一个用于 FTP。当 FTP 到我的服务器时,结果仍然导致文件大小为 0。
因为BitmapImage
读到流的末尾,所以FtpClient
没有数据到read/upload。
使用Stream.Seek
将流指针重置回开头。
G.myStream.Seek(0, SeekOrigin.Begin);