将图像保存为varbinary(第3部分) [英] Saving image to database as varbinary (part 3)

查看:194
本文介绍了将图像保存为varbinary(第3部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这又是一个问题,我将Silverlight中的图像保存到我的数据库中,我以为我把它全部工作了,直到我用不同的图像试了一下......

This is yet again a problem I have with saving images in silverlight to my database, I thought I had it all working untill I tried it out with a different image...

我使用以下方法将图像保存到我的数据库。
我首先将图像转换为 byte 的数组,然后将其发送到我的服务。

I save images to my database with following method. I first convert the image to an array of byte and then send it to my service.

 private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            //nieuwe instantie van de klasse "Afbeelding", om later door te sturen naar service
            Afbeelding a = new Afbeelding();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "JPEG files|*.jpg";

            if (openFileDialog.ShowDialog() == true)
            {
                //Afbeelding ophalen via open dialoog
                Stream stream = (Stream)openFileDialog.File.OpenRead();
                string fileName = openFileDialog.File.Name;

                //Converteren naar bytes
                //byte[] bytes = BinaryConverter.convertToByte(stream);
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);

                //aan de instantie de Binary waarde van de afbeelding meegeven om naar de database te sturen
                a.id = 1;
                a.source = new Binary { Bytes = bytes };
            }

            EditAfbeeldingServiceClient client = new EditAfbeeldingServiceClient();

            client.UpdateAfbeeldingCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateAfbeeldingCompleted);
            client.UpdateAfbeeldingAsync(a);
        }

在我的服务中我这样做:

And in my service I do this:

    [OperationContract]
    public void UpdateAfbeelding(Afbeelding a)
    {
        var query = (from p in dc.Afbeeldings 
                     where p.id == a.id
                     select p).SingleOrDefault();

        query.source = a.source;
        dc.SubmitChanges();

    }

现在我的测试中这一切都有效,但我只用过一个要测试的图像...
所以当我刚才尝试使用不同的图像时,我收到以下错误:

Now during my testing this all worked, but I only used one image to test... So when I tried just now with a different image, I get the following error:

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (400) Bad Request. In Silverlight, a 404 response code may be reported even when the service sends a different error code. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndUpdateAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndUpdateAfbeelding(IAsyncResult result)
   at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndUpdateAfbeelding(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

我无法真正读出任何错误,所以再一次,我被困在这里。
我为使用这些电路板而道歉,但如果不需要这么多,我真的不会。

I can't really read anything out of that error so once again, I'm stuck here. I apologise for using these boards so much, but I really wouldn't if it wasn't needed so much.

我设置了发送的最大值到一个很高的数字,但它仍然不起作用。

I have set the maximum to send through to a high number, but it still doesn't work.

<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />

您可以在此处找到我的web.config: http://pastebin.com/whMs5h1w

You can find my web.config here: http://pastebin.com/whMs5h1w

感谢您的帮助,我真的很感激。
Thomas

Thank you for your help, I really appreciate it. Thomas

编辑:我设法通过启用跟踪获得更可读的错误,希望这有助于任何人:)

I managed to get a more readable error with enabling tracing, hope this helps anyone :)

推荐答案

WCF内置了各种限制。一个是 maxReceivedMessageSize ,默认为65536字节,另一个为 maxArrayLength (不确定默认值是多少)。你有可能超过两者中的一员(或两者兼而有之)。您可以更改服务配置中的那些。 MSDN上的这篇文章包含一些示例配置。

WCF has various limits built in. One is the maxReceivedMessageSize which is 65536 bytes by default and another one is maxArrayLength (not sure what the default is). There is a good chance you have exceeded one of the two (or both). You can change those in your service configuration. This article on MSDN contains some example configurations.

另外启用对您服务的跟踪可能会为您提供一些更深入了解哪些限制的信息。

Also enabling tracing for your service might provide you with some more insight of which limits are hit.

Btw:有一个 File.ReadAllBytes method。

Btw: There is a File.ReadAllBytes method.

编辑:显然有一个名为 Fiddler ,可以帮助追踪这些问题。

Edit: Apparently there is a tool called Fiddler which can help tracking these issues down.

这篇关于将图像保存为varbinary(第3部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆