我想通过WCF服务将图像从WP页面上传到数据库。我有以下服务实现编译好:
public bool Upload(Stuff picture)
{
FileStream fileStream = null;
BinaryWriter writer = null;
string filePath;
try
{
filePath = HttpContext.Current.Server.MapPath(".") +
ConfigurationManager.AppSettings["PictureUploadDirectory"] +
picture.stuffName;
if (picture.stuffName != string.Empty)
{
fileStream = File.Open(filePath, FileMode.Create);
writer = new BinaryWriter(fileStream);
writer.Write(picture.stuffPhoto);
}
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (fileStream != null)
fileStream.Close();
if (writer != null)
writer.Close();
}
}
但在WP页面上我有错误。我正在学习本教程:http://www.silverlightshow.net/items/Uploading-and-downloading-images-from-WCF-in-Silverlight.aspx这是我的代码:
private void uploadBtn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog(); // error 1,2
openFileDialog.Filter = "JPEG files|*.jpg";
if (openFileDialog.ShowDialog() == true)
{
Stream stream = (Stream)openFileDialog.File.OpenRead();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
string fileName = openFileDialog.File.Name;
ServiceReference1.Stuff pictureFile = new ServiceReference1.Stuff();
ServiceReference1.Stuff.stuffName = fileName; //error 3,4
ServiceReference1.Stuff.stuffPhoto = bytes;
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.UploadCompleted += new EventHandler
<System.ComponentModel.AsyncCompletedEventArgs>(client_UploadCompleted); // error 5
client.UploadAsync(pictureFile);
}
}
void client_UploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result) // error 6
{
ResultTextlock.Text = "Upload succeeded :)";
}
else
{
ResultTextBlock.Text = "Upload failed :(";
}
}
}
错误是:
1,2)无法找到类型或命名空间名称'OpenFileDialog'(您是否缺少using指令或程序集引用?)。
3,4)非静态字段,方法或属性'PhoneApp1.ServiceReference1.Stuff.stuffName.get'
需要对象引用5)无法将类型'System.EventHandler'隐式转换为'System.EventHandler'
6)'System.ComponentModel.AsyncCompletedEventArgs'不包含'Result'的定义,并且没有扩展方法'Result'可以找到接受类型'System.ComponentModel.AsyncCompletedEventArgs'的第一个参数(你是否错过了使用指令或程序集引用?)
7)名称空间'System.Windows'中不存在类型或命名空间名称'Forms'(您是否缺少程序集引用?)(我有tis语句:使用System.Windows.Forms;)`
答案 0 :(得分:0)
Windows Phone没有OpenFileDialog,您必须使用Photo Chooser task
您正在引用ServiceReference1.Stuff
作为静态对象。我认为你的意思是pictureFile.stuffName
等。
事件的类型错误,如错误消息所示。
Windows Phone没有Windows窗体可用。
您无法将代码从Silverlight复制/粘贴到Windows Phone并假设它有效。您必须学习如何使用WP。这些也是错误消息中清楚解释的基本错误。我建议了解基础知识。