Saving image to database as varbinary, arraylength (part 2)

时光毁灭记忆、已成空白 提交于 2019-12-12 02:52:36

问题


This is a followup to my previous question, which got solved (thank you for that) but now I am stuck at another error.

I'm trying to save an image in my database (called 'Afbeelding'), for that I made a table which excists of:

  • id: int
  • souce: varbinary(max)

I then created a wcf service to save an 'Afbeelding' to the database.

    private static DataClassesDataContext dc = new DataClassesDataContext();

    [OperationContract]
    public void setAfbeelding(Afbeelding a)
    {
        //Afbeelding a = new Afbeelding();
        //a.id = 1;
        //a.source = new Binary(bytes);

        dc.Afbeeldings.InsertOnSubmit(a);
        dc.SubmitChanges();
    }

I then put a reference to the service in my project and when I press the button I try to save it to the datbase.

    private void btnUpload_Click(object sender, RoutedEventArgs e)
    {

        Afbeelding a = new Afbeelding();

        OpenFileDialog openFileDialog = new OpenFileDialog();

        openFileDialog.Filter = "JPEG files|*.jpg";

        if (openFileDialog.ShowDialog() == true)
        {
                //string imagePath = openFileDialog.File.Name;
                //FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                //byte[] buffer = new byte[fileStream.Length];
                //fileStream.Read(buffer, 0, (int)fileStream.Length);
                //fileStream.Close();

            Stream stream = (Stream)openFileDialog.File.OpenRead();
            Byte[] bytes = new Byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);
            string fileName = openFileDialog.File.Name;


            a.id = 1;
            a.source = new Binary { Bytes = bytes };

        }


        EditAfbeeldingServiceClient client = new EditAfbeeldingServiceClient();

        client.setAfbeeldingCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_setAfbeeldingCompleted);
        client.setAfbeeldingAsync(a);
    }

    void client_setAfbeeldingCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
            txtEmail.Text = e.Error.ToString();
        else
            MessageBox.Show("WIN");
    }

However, when I do this, I get the following error:

    System.ServiceModel.FaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :a. 
    The InnerException message was 'There was an error deserializing the object of type OndernemersAward.Web.Afbeelding. 
    The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'.  
    Please see InnerException for more details.    
    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)    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)  
  atOndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndsetAfbeelding(IAsyncResult result)
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndsetAfbeelding(IAsyncResult result)
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndsetAfbeelding(IAsyncResult result)    
    at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

I'm not sure what's causing this but I think it has something to do with the way I write the image to the database? (The array length is too big big, I don't really know how to change it)

Thank you for your help, Thomas


回答1:


looks like you need to change default reader quotas in binding, set lengths to appropriate values for you:

 <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/>


来源:https://stackoverflow.com/questions/8275398/saving-image-to-database-as-varbinary-arraylength-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!