通过 WCF 上载数据库中的图像



我想通过 WCF 上传数据库中的图像。我正在开发一个WP 8应用程序,该应用程序将在数据库中上传图像,然后将其显示在用户的个人资料页面上。除了本教程之外,没有找到与WP相关的任何内容。我正在尝试遵循它,但出现错误。这是我的服务实现:

public bool Upload(Stuff picture)
    {
        Stuff stPic = new Stuff();
        stPic.stuffName fileStream = null; //error 1
        stPic.stuffPhoto writer = null; 
        string filePath;
        try
        {
            filePath = HttpContext.Current.Server.MapPath(".") +
                       ConfigurationManager.AppSettings["PictureUploadDirectory"] +
                       picture.stuffName; //error 2,3
            if (picture.stuffName != string.Empty)
            {
                fileStream = File.Open(filePath, FileMode.Create);//error 4,5
                writer = new BinaryWriter(fileStream); //error 6
                writer.Write(picture.stuffPhoto);
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            if (fileStream != null)
                fileStream.Close();
            if (writer != null)
                writer.Close();
        }
    }

错误:

1(找不到类型或命名空间名称"stPic"(您是 缺少 using 指令或程序集引用?

2(名称"HttpContext"在当前上下文中不存在。

3( 名称"配置管理器"在当前上下文中不存在。

4( 名称"文件"在当前上下文中不存在

5( "文件模式"的名称>当前上下文中不存在

6( 找不到命名空间名称"BinaryWriter"的类型或> (是否缺少 using 指令或程序集引用?

7( DBML1005:DbType 'VarBinary(MAX(' 和 Type 之间的映射 不支持类型为"stuff"的"stuffPhoto"列中的"System.Data.Linq.Binary"。

编辑:添加了使用语句,因此错误2,3,4,5,6消失了。

using System.Web;
using System.Configuration;
using System.IO;

更改了教程中的代码(错误 1 消失了(。

FileStream fileStream = null;
BinaryWriter writer = null;

但是这行代码writer.Write(picture.stuffPhoto);突出显示,错误是:1) The best overloaded method match for 'System.IO.BinaryWriter.Write(string)' has some invalid arguments. 2) Argument 1: cannot convert from 'System.Data.Linq.Binary' to 'string' .

我还尝试将数据库中stuffPhoto字段的类型更改为image并再次创建LINQ to SQL Classes。但得到相同类型的错误:DBML1005: Mapping between DbType 'Image' and Type 'System.Data.Linq.Binary' in Column 'stuffPhoto' of Type 'Stuff' is not supported.

编辑:我通过在xml编辑器中打开dbml文件并将System.Data.Linq.Binary类型替换为System.Byte[]来修复第7个错误。结果,上面的两个错误也消失了。所以现在 Web 服务编译没有错误。

stPic.stuffName fileStream = null; //error 1

除非你在从FileStream继承的类stPic中定义了一个名为stuffName的嵌套类型,否则(这似乎不太可能!(这段代码没有意义,因为你似乎试图同时设置一个名为stuffName的属性和一个名为fileStream的变量。您链接到的教程创建了一个带有FileStream fileStream = null; FileStream,为什么不这样做呢?

 stPic.stuffPhoto writer = null;

我希望这会给出与上述相同的错误。

对于错误 3、4、5 和 6,请在项目中添加对 System.WebSystem.ConfigurationSystem.IO 的引用。为了访问HttpContext您需要将其添加到您的 web.config 文件中:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">

您尚未列出给出错误 7 的代码。

最新更新