2016-01-08 4 views
9

मैं एक वेबसर्वर का उपयोग कर स्थानीयहोस्ट में एचटीएमएल 5 कैनवास सामग्री को सहेजने की कोशिश कर रहा हूं। मुझे बेस 64 में कैनवास मान मिल रहा है और मैं इसे अपने webservice पर भेजता हूं। लेकिन जब मैं वेब सेवा को डेटा भेजने मैं इस त्रुटि मिलती है और फ़ाइल सहेजा नहीं गया है:वेबसाइट 64 पर बेस 64 में छवि भेजना - 'एप्लिकेशन/ऑक्टेट-स्ट्रीम' अपेक्षित प्रकार 'टेक्स्ट/एक्सएमएल नहीं था; charset = utf-8 '

415: "संदेश संसाधित नहीं कर सकता क्योंकि सामग्री प्रकार 'आवेदन/ओकटेट धारा' की उम्मीद नहीं कर रहा था टाइप करें 'टेक्स्ट/एक्सएमएल; charset = utf-8'। "

मैं क्या गलत कर रहा हूं?

Service.vb

Imports System.IO 
Imports System.Drawing 

Public Class Service 
    Implements IService 

    Public Sub New() 
    End Sub 


    Public Function savePictureBase64(bytes As Byte()) As Boolean Implements IService.savePictureBase64 
     Dim fullOutputPath As String = "c:\temp\file.png" 

     'get a temp image from bytes, instead of loading from disk 
     'data:image/gif;base64, 


     Dim imagem As Image 
     Using ms As New MemoryStream(bytes) 
      imagem = Image.FromStream(ms) 
     End Using 

     File.WriteAllBytes(fullOutputPath, (bytes)) 

     Return True 


    End Function 

End Class 

IService.vb

<ServiceContract()> 
Public Interface IService 


    <OperationContract()> 
    Function savePictureBase64(bytes As Byte()) As Boolean 



    ' TODO: Add your service operations here 

End Interface 

जावास्क्रिप्ट

function save() { 
       var image = document.getElementById("sketchpad").toDataURL("image/png"); 
       image = image.replace('data:image/png;base64,', ''); 
       $.ajax({ 
      type: 'POST', 
      url: 'http://localhost:52193/service.svc', 
      data: image, 
      contentType: 'application/octet-stream', 
       success: function (msg) { 
         alert('Image saved successfully !'); 
       }, 
       error: function(result) { 
        alert("Error"); 
       } 
      }); 
     } 

</script> 

web.config

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5"/> 
    <httpRuntime targetFramework="4.5"/> 
    <pages> 
     <namespaces> 
     <add namespace="System.Runtime.Serialization"/> 
     <add namespace="System.ServiceModel"/> 
     <add namespace="System.ServiceModel.Web"/> 
     </namespaces> 
    </pages> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <basicHttpBinding> 
     <binding messageEncoding="Mtom"> 
     </binding> 
     </basicHttpBinding> 
    </bindings>  
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 
+1

संबंधित: [त्रुटि वेब सेवा लेने वाली, सामग्री प्रकार "application/XOP + xml" उम्मीद प्रकार "text/xml" से मेल नहीं खाता ] (http://stackoverflow.com/questions/10496186/error-consuming-webservice-content-type-application-xopxml-does-not-match-ex)। – cybermonkey

+0

जोड़ा गया web.config पर। अभी भी काम नहीं कर रहे – RSilva

+1

इसे आज़माएं: 'डेटा: {बाइट्स: छवि} 'और लाइन' सामग्री टाइप करें 'टाइप करें:' एप्लिकेशन/ऑक्टेट-स्ट्रीम ' – Hackerman

उत्तर

4

त्रुटि वह कॉल है जिसे आपने जावास्क्रिप्ट कोड द्वारा बनाया था। आप एक स्ट्रिंग भेजने का प्रयास करते हैं, webservice को एक XML संदेश की उम्मीद है:

अपेक्षित प्रकार 'टेक्स्ट/xml; charset = utf-8 '।

मुझे नहीं पता कि जावास्क्रिप्ट से एक webservice XML संदेश लिखना कितना जटिल है, लेकिन मुझे लगता है कि आप अपना दृष्टिकोण बदल सकते हैं। आपकी सेवा आईआईएस के तहत होस्ट की जाती है, क्या आप एक एचटीपी हैंडलर बना सकते हैं?

public class UploadBase64 : IHttpHandler 
{ 
    public bool IsReusable 
    { 
     get { return true; } 
    } 
    public void ProcessRequest(HttpContext context) 
    { 
     string image_string = string.Empty; 
     using (StreamReader sr = new StreamReader(context.Request.InputStream)) 
      image_string = sr.ReadToEnd(); 
     byte[] image_bin = Convert.FromBase64String(image_string); 
     File.WriteAllBytes(@"c:\temp_10\test01.png", image_bin); 
    } 
} 

... और अपने web.config फ़ाइल से जोड़ें:

<system.web> 
    <httpHandlers> 
    <add verb="POST" path="UploadBase64.aspx" type="WebApplication1.UploadBase64"/> 
    </httpHandlers> 
</system.web> 
संबंधित मुद्दे