這是又一次的問題我有一個保存在Silverlight圖片到我的數據庫,我想我擁有了一切工作,直到我嘗試過了一個不同的圖像...保存圖像數據庫爲varbinary(第三部分)
我使用以下方法將圖像保存到我的數據庫。 我首先將圖像轉換爲byte
的數組,然後將其發送給我的服務。
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);
}
而在我的服務我這樣做:
[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();
}
在我的測試中這一切工作
現在,但我只用了一個形象來測試... 所以,當我試圖剛纔用不同的圖像,我得到以下錯誤:
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)
我真的不能讀取任何錯誤,所以再次,我卡在這裏。 我很抱歉使用這些電路板,但如果不需要這麼多,我真的不會這麼做。
我已經設置了最大通過發送給大量的,但它仍然無法正常工作。
<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
你可以在這裏找到我的web.config:http://pastebin.com/whMs5h1w
謝謝您的幫助,我真的很感激。 托馬斯
編輯:我得到了一個更可讀的錯誤與啓用跟蹤,希望這可以幫助任何人:)
那我該怎麼辦?我無法使用WCF將圖像保存到數據庫中? – Schoof
@ThomasSchoof:不,您需要增加配置中的限制。對不起,答案更清楚了。 – ChrisWue
我已經不得不增加maxArrayLength,因爲我得到了以前的一個錯誤,那個人說我不得不放大它。 (http://stackoverflow.com/questions/8275398/saving-image-to-database-as-varbinary-arraylength-part-2/8275697#8275697) 我可以只使用File.ReadAllbytes方法來轉換圖像到一個字節數組? – Schoof