Windows phone 7 如何从windows phone向服务器上载和发送文件和字符串数据?
我需要从windows phone向服务器发送文件和xml字符串 下面是服务器端IhttpHandler代码 现在请给我一些windows phone的代码示例,1个大文件可以上传到服务器,还可以通过POST上传xml或任何字符串 下面是服务器端代码Windows phone 7 如何从windows phone向服务器上载和发送文件和字符串数据?,windows-phone-7,Windows Phone 7,我需要从windows phone向服务器发送文件和xml字符串 下面是服务器端IhttpHandler代码 现在请给我一些windows phone的代码示例,1个大文件可以上传到服务器,还可以通过POST上传xml或任何字符串 下面是服务器端代码 <%@ WebHandler Language="C#" Class="Test" %> using System; using System.Web; using System.IO; public class Test :
<%@ WebHandler Language="C#" Class="Test" %>
using System;
using System.Web;
using System.IO;
public class Test : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(AddSurveyMediaWithFile(context.Request.Files[0] , context.Request["MediaDataXML"].ToString()));
}
public bool IsReusable {
get {
return false;
}
}
public string ShowMessage(string msg)
{
return msg;
}
public String AddSurveyMediaWithFile(HttpPostedFile uploadedFile, string MediaDataXML)
{
try
{
Survey.AddDataMediaXMLCheck(MediaDataXML, "Media");
string FilePath = HttpContext.Current.Server.MapPath("../MediaFiles/");
AttributeValue objAttributeValue = ParseValue(MediaDataXML);
objAttributeValue.Value = objAttributeValue.Value;
OrganizationUnitLicense objLicense = OrganizationUnitLicense.ValidateFilesize(objAttributeValue.UserWorkUnitSurveyID);
if (objAttributeValue.Filesize > 0)
{
if (objLicense.Space > 0)
{
if ((objLicense.TotalFilesize + objAttributeValue.Filesize) <= (objLicense.Space * 1024 * 1024))
{
string msg = Survey.AddSurveyData(MediaDataXML);
if (msg == Enumerators.SQLReturn.SUCCESS.ToString())
{
if (File.Exists(FilePath + objAttributeValue.Value))
File.Delete(FilePath + objAttributeValue.Value);
uploadedFile.SaveAs(FilePath + objAttributeValue.Value);
return Enumerators.SQLReturn.SUCCESS.ToString();
}
else
{
return msg;
}
}
else
{
return "FILESIZE_" + Enumerators.SQLReturn.LIMIT_NOT_EXIST.ToString();
}
}
else
{
string msg = Survey.AddSurveyData(MediaDataXML);
if (msg == Enumerators.SQLReturn.SUCCESS.ToString())
{
if (File.Exists(FilePath + objAttributeValue.Value))
File.Delete(FilePath + objAttributeValue.Value);
uploadedFile.SaveAs(FilePath + objAttributeValue.Value);
return Enumerators.SQLReturn.SUCCESS.ToString();
}
else
{
return msg;
}
}
}
else
{
return "FILESIZE_ZERO";
}
}
catch (Exception ex)
{
return ex.Message + Environment.NewLine + ex.StackTrace; //Enumerators.SQLReturn.ERROR.ToString();
}
}
private AttributeValue ParseValue(string MediaDataXML)
{
..... //to do section
}
}
使用制度;
使用System.Web;
使用System.IO;
公共类测试:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
context.Response.ContentType=“text/plain”;
context.Response.Write(AddSurveyMediaWithFile(context.Request.Files[0],context.Request[“MediaDataXML”].ToString());
}
公共布尔可重用{
得到{
返回false;
}
}
公共字符串ShowMessage(字符串消息)
{
返回味精;
}
公共字符串AddSurveyMediaWithFile(HttpPostedFile uploadedFile,字符串MediaDataXML)
{
尝试
{
AddDataMediaXMLCheck(MediaDataXML,“媒体”);
字符串FilePath=HttpContext.Current.Server.MapPath(“../MediaFiles/”);
AttributeValue OBJAAttributeValue=ParseValue(MediaDataXML);
objAttributeValue.Value=objAttributeValue.Value;
OrganizationUnitLicense objLicense=OrganizationUnitLicense.ValidateFilesize(objAttributeValue.UserWorkUnitSurveyID);
如果(objAttributeValue.Filesize>0)
{
如果(objLicense.Space>0)
{
如果((objLicense.TotalFilesize+objAttributeValue.Filesize)我没有读你的代码,但我会回答你的问题
将字符串数据上载到服务器:
Public Sub UploadFile(xmlPath As String, type As String)
Try
Dim webClient As New WebClient()
webClient.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
Dim uri = New Uri(xmlPath, UriKind.Absolute)
Dim postData As New StringBuilder()
postData.AppendFormat("{0}={1}", "YOUR_PARAMETER_HERE", "")
webClient.Headers(HttpRequestHeader.ContentLength) = postData.Length.ToString()
AddHandler webClient.UploadStringCompleted, AddressOf webClient_UploadStringCompleted
webClient.UploadStringAsync(uri, "POST", postData.ToString())
Catch ex As Exception
LogError(ex)
End Try
End Sub
Private Sub webClient_UploadStringCompleted(sender As Object, e As UploadStringCompletedEventArgs)
Try
Dim jsonString = e.Result
//PUT YOUR OWN CODE HERE
Catch ex As Exception
LogError(ex)
End Try
End Sub
然后您需要在此处处理响应:
Public Sub UploadFile(xmlPath As String, type As String)
Try
Dim webClient As New WebClient()
webClient.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
Dim uri = New Uri(xmlPath, UriKind.Absolute)
Dim postData As New StringBuilder()
postData.AppendFormat("{0}={1}", "YOUR_PARAMETER_HERE", "")
webClient.Headers(HttpRequestHeader.ContentLength) = postData.Length.ToString()
AddHandler webClient.UploadStringCompleted, AddressOf webClient_UploadStringCompleted
webClient.UploadStringAsync(uri, "POST", postData.ToString())
Catch ex As Exception
LogError(ex)
End Try
End Sub
Private Sub webClient_UploadStringCompleted(sender As Object, e As UploadStringCompletedEventArgs)
Try
Dim jsonString = e.Result
//PUT YOUR OWN CODE HERE
Catch ex As Exception
LogError(ex)
End Try
End Sub
关于上传文件,我建议您阅读以下答案: