不会用。NET· 最近需要做一个需求,就是VC下上传文件数据给asp.net。
客户端采用POST提交了一串数据,数据格式大致是:
guid=xxxx-xxxx-xxxx&filename=xxxxx.xx&filecontent=*****************************
这样。
在asp.net中的page_load方法里,用Request读取数据发现有些问题。
因为文件内容可能包含有0,常用的以string来接受数据就有问题了,会截断。
用Request.BinatyRead读取的话,都读取到一个byte数组里,如何把我需要的filecontent部分取出来就是个问题了,因为文件内容包含的字符可能就有前面的部分,不能简单的以字符来分割了。
如何获取这段数据呢?
------解决方案--------------------------------------------------------
吧你需要传输的数据 urldecode一次
------解决方案--------------------------------------------------------
你用 Request.QueryString[""].toString()不行么?
------解决方案--------------------------------------------------------
string filecontent =Server.UrlDecode(Request.QueryString["filecontent"])应该可以实现吧
------解决方案--------------------------------------------------------
弱弱的问下。 post的话 为啥不直接获取文件信息。。 而去弄string呢。
------解决方案--------------------------------------------------------
将接受的字符串按照 &分组呢?
string[] s=result.spilt('&');
然后通过这个数组去获得呢?
------解决方案--------------------------------------------------------
在客户端对filecontent做base64编码,在服务器端再解码
------解决方案--------------------------------------------------------
这是别人提交短信到我们服务器的方法,也是post提交的,跟你描述的需求一样,思路是先获取,然后用XML解析,希望有用。
- C# code
protected void Page_Load(object sender, EventArgs e) { Stream instream = Request.InputStream; BufferedStream buf = new BufferedStream(instream); byte[] buffer = new byte[1024]; string str = ""; int a; while ((a = buf.Read(buffer, 0, buffer.Length)) != 0) { str = str + System.Text.Encoding.GetEncoding("GB2312").GetString(buffer); } int returnval = 9; if (!string.IsNullOrEmpty(str)) { returnval = parseXML(str.Trim()); } Response.Write(returnval.ToString()); sw.Flush(); sw.Close(); GC.Collect(); Response.End(); } private int parseXML(string xmlstr, StreamWriter sw) { int val = 9; try { XmlDocument document = new XmlDocument(); document.LoadXml(xmlstr); XmlElement element = document.DocumentElement; for (int i = 0; i < element.ChildNodes.Count; i++) { XmlNode node = element.ChildNodes[i]; string corp_id = node["corp_id"].InnerText;//用户名id string mobile = node["mobile"].InnerText;//号码 string ext = node["ext"].InnerText;//小号 string time = node["time"].InnerText;//时间 string content = node["content"].InnerText;//内容 ReceiveMsgInfo info = new ReceiveMsgInfo(); info.Mobile = mobile; info.Content = content; info.Status = 0; info.Recetime = DateTime.Parse(time); info.Userport = Convert.ToInt32(ext);//小号 int sussress = Controller.AddReceiveInfo(info);//保存到数据库当中 } val = 0; } catch (Exception exc) { } return val; }