从WP 8页面上传图像到数据库

本文关键字:图像 数据库 WP 8页 | 更新日期: 2023-09-27 18:08:56

我想通过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' to 'System。EventHandler '

System.ComponentModel

6)"。AsyncCompletedEventArgs'不包含'Result'的定义,也没有扩展方法'Result'接受类型为'System.ComponentModel '的第一个参数。可以找到AsyncCompletedEventArgs'(您是否缺少using指令或程序集引用?)

7)命名空间"System"中不存在类型或命名空间"Forms"。Windows'(您是否缺少汇编引用?)(我有这个语句:using System.Windows.Forms;) '

从WP 8页面上传图像到数据库

Windows Phone没有OpenFileDialog,你必须使用照片选择器任务

您将ServiceReference1.Stuff引用为静态对象。我想你是指pictureFile.stuffName等。

事件类型错误,如错误消息所示。

Windows Phone没有可用的Windows窗体。

你不能只是复制/粘贴代码从Silverlight到Windows Phone,并认为它可以工作。你必须学习如何使用WP。此外,这些是在错误消息中清楚解释的基本错误。