Hi Joe, I have a problem:
I'm trying to write a webservice that allows uploading of images. I used your javascript to create a soap message w/ the image. here my clientside code (it create a soap message) and it works:
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
function CreateXml()
{
var ostream=new ActiveXObject("ADODB.Stream");
ostream.Type=1;
alert(txtFile.value);
ostream.Open();
ostream.LoadFromFile(txtFile.value);
var senv="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
senv+="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
senv+="<soap:Body>\n";
senv+="<upload xmlns=\"http://localhost/wsUpload\">\n";
senv+="<imageName>" + txtFile.value + "</imageName>\n";
senv+="<imgdata></imgdata>\n";
senv+="</upload>\n";
senv+="</soap:Body>\n";
senv+="</soap:Envelope>\n";
xmlhttp.Open("POST","http://localhost/wsUpload/UploadService.asmx",false);
xmlhttp.setRequestHeader("Content-Type","text/xml");
This is my webservice method:
[WebMethod(Description="upload images")]
public void upload(String imageName,string imgdata)
{
//int Array
try
{
String fileLoc=@"c:\temp\";
FileStream fs=File.Create(fileLoc + "test.jpg");
byte[] data=Convert.FromBase64String(imgdata);
fs.Write(data,0,data.GetUpperBound(0) + 1);
fs.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}
}
xmlhttp.setRequestHeader("SOAPAction","\"http://localhost/wsUpload/upload\"");
var oDoc=new ActiveXObject("Microsoft.XMLDOM");
oDoc.loadXML(senv);
var oNode=oDoc.selectSingleNode("//imgdata");
oNode.dataType="bin.base64";
oNode.nodeTypedValue=ostream.Read();
Textarea1.value=oDoc.xml;
xmlhttp.Send(senv);
ostream.Close();
}
This is my webservice method:
[WebMethod(Description="upload images")]
public void upload(String imageName,string imgdata)
{
//int Array
try
{
String fileLoc=@"c:\temp\";
FileStream fs=File.Create(fileLoc + "test.jpg");
byte[] data=Convert.FromBase64String(imgdata);
fs.Write(data,0,data.GetUpperBound(0) + 1);
fs.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}
}
when I run it, the client side call the webservice method successfully, but only the imageName is passed to the method and NOT the imgdata.
Please help
Eric
Quote:
quote:Originally posted by joefawcett
Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.
Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "word.xml"
function main()
{
var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
oDoc.async = false;
var oStream = new ActiveXObject("ADODB.Stream");
oStream.type = 1; //Binary
oStream.open();
oStream.loadFromFile(WORD_PATH);
var oNode = oDoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
oDoc.insertBefore(oNode, oDoc.childNodes[0]);
var oRoot = oDoc.createElement("Root")
oDoc.documentElement = oRoot
oRoot.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
oNode = oRoot.appendChild(oDoc.createElement("wordDoc"));
oNode.dataType = "bin.base64";
oNode.nodeTypedValue = oStream.read();
oStream.close();
oDoc.save(XML_PATH);
}
main();
second file
Code:
//XmlToBinary.js
var WORD_PATH = "hello2.doc";
var XML_PATH = "word.xml"
function main()
{
var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
oDoc.async = false;
oDoc.load(XML_PATH);
var oStream = new ActiveXObject("ADODB.Stream");
oStream.type = 1; //Binary
oStream.open();
oStream.write(oDoc.documentElement.selectSingleNode("wordDoc").nodeTypedValue);
oStream.savetoFile(WORD_PATH);
oStream.close();
}
main();
If you have trouble converting to VB let me know.
--
Joe
|