HY。
我在WP7中执行一个应用程序,它是一个FTP服务器。我想上传一张照片(带有photochoosertask)。
我写了一个PhotoChooserTask(),我可以选择一张照片。程序保存照片名称(samplephoto01.jpg)和照片路线。 我写了一个代码,将命令发送到FTP服务器:
public static void Execute(String msg)
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
Byte[] cmd = Encoding.UTF8.GetBytes((msg + "\r\n").ToCharArray());
socketEventArg.SetBuffer(cmd, 0, cmd.Length);
socket.SendAsync(socketEventArg);
}
这段代码我可以选择照片:
public void SelectAndUpLoad()
{
PhotoChooserTask p = new PhotoChooserTask();
p.Completed += new EventHandler<PhotoResult>(pt_Completed);
p.ShowCamera = true;
p.Show();
}
void pt_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ChosenPhoto);
MediaLibrary library = new MediaLibrary();
string PhotoPath = e.OriginalFileName;
// MessageBox.Show(PhotoPath);
for (int i = 0; i < library.Pictures.Count; i++)
{
Stream s = library.Pictures[i].GetImage();
if (s.Length == e.ChosenPhoto.Length)
{
string filename = library.Pictures[i].Name;
MessageBoxResult m = MessageBox.Show(filename, "Upload?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
Ftp.UploadFile(PhotoPath);
}
else
{
return;
}
break;
}
}
}
}
这是我想上传文件的代码:
public static void UploadFile(string file)
{
FileStream stream = new FileStream(file, FileMode.Open);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + file);
stream.Seek(0, SeekOrigin.Begin);
stream.Close();
}
但是当我使用UploadFile();方法程序回答这个:
MethodAccessException未处理
此代码:
.
.
Ftp.UploadFile(PhotoPath);
}
else
{ //MethodAccessException
return;
}
break;
}
出了什么问题?谢谢!
我用IsolatedStorage将此代码重写为:
for (int i = 0; i < library.Pictures.Count; i++)
{
Stream s = library.Pictures[i].GetImage();
if (s.Length == e.ChosenPhoto.Length)
{
string filename = library.Pictures[i].Name;
MessageBoxResult m = MessageBox.Show(filename, "Upload?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
IsolatedStorageFile iss = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fs = iss.OpenFile(PhotoPath, FileMode.Open);
Ftp.UploadFile(fs, filename);
fs.Close();
}
else
{
return;
}
break;
}
}
上传文件()方法:
public static void UploadFile(IsolatedStorageFileStream file, string RemoteFile)
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
int bytes;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + RemoteFile);
file.Seek(0, SeekOrigin.Begin);
while ((bytes = file.Read(buffer, 0, buffer.Length)) > 0)
{
socketEventArg.SetBuffer(buffer, bytes, 0);
socket.SendAsync(socketEventArg);
}
}
但是我在这个来源中得到了一个例外:
IsolatedStorageFileStream fs = iss.OpenFile(PhotoPath, FileMode.Open);
例外情况是:IsolatedStorageException未被禁用。
有什么问题?
答案 0 :(得分:1)
我认为你的问题在于:
FileStream stream = new FileStream(file, FileMode.Open);
你无法在WP7上以这种方式打开文件。要获取文件流,您可以从隔离存储中打开它(假设文件存储在那里),或者使用内置方法提供的流。
在您的情况下,您拥有属性e.ChosenPhoto
的流。你为什么不直接使用它?
public static void UploadFile(Stream stream, string file)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + file);
stream.Seek(0, SeekOrigin.Begin);
stream.Close();
}
然后使用UploadFile
作为第一个参数调用e.ChosenPhoto
。