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

时间:2014-06-15 16:46:47

标签: c# sql linq wcf visual-studio

我想通过WCF在数据库中上传图片。我正在构建一个WP 8应用程序,它将在数据库中上传图像,然后在用户的个人资料页面上显示它。除了this tutorial之外,没有找到与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)当前上下文中不存在名称“ConfigurationManager”。

     

4)当前上下文中不存在名称“文件”

     

5)名称> “FileMode”在当前上下文中不存在

     

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

     

7)DBML1005:DbType'VarBinary(MAX)'和Type之间的映射   不支持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服务编译没有错误。

1 个答案:

答案 0 :(得分:0)

stPic.stuffName fileStream = null; //error 1

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

 stPic.stuffPhoto writer = null;

我希望能给出与上面相同的错误。

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

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">

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