>我需要将我的二进制文件上传到服务器然后>
>从服务器获取答案也作为二进制文件
我目前使用的样本是这样的:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx
但是样本以请求开头,我需要从上传开始.
我尝试过使用WebClIEnt,但是我无法按顺序执行上传/下载:Bidirectional use of Webclient for binary
解决方法 AHSX是Go的方式,但特别考虑超过4 MB的文件.你需要做什么两件事:
>服务器端:将处理收到的数据的Ashx httpHandler:
公共类fileUpload:IhttpHandler
{
public voID ProcessRequest(httpContext context)
{
_httpContext = context;
if (context.Request.inputStream.Length == 0) throw new ArgumentException("No file input"); try { // Method that will be populating parameters from httpHandler Request. Getquerystringparameters(); string uploadFolder = _directory; string tempfilename = _filename + _tempExtension; if (_firstChunk) { //// Delete temp file if (file.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempfilename)) file.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempfilename); //// Delete target file if (file.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _filename)) file.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _filename); } using (fileStream fs = file.Open(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempfilename,fileMode.Append)) { Savefile(context.Request.inputStream,fs); fs.Close(); } if (_lastChunk) { file.Move(HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempfilename,HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _filename); } } catch (Exception e) { // Todo // Logger Exception throw; }}
}
>将调用它的Silverlight类:
公共类Uploadfile
{
public voID Upload(Stream streamNewfile,string filename,string dirOnServer =“”)
{
this.uploadMode = UploadMode.OpenStream;
this.destinationOnServer = dirOnServer; this.fileStream = streamNewfile; this.filename = filename; this.dataLength = this.fileStream.Length; long dataToSend = this.dataLength - this.dataSent; bool isLastChunk = dataToSend <= this.chunkSize; bool isFirstChunk = this.dataSent == 0; string DOCTYPE = "document"; var strCurrentHost = HTMLPage.document.documentUri.ToString().Substring(0,HTMLPage.document.documentUri.ToString().LastIndexOf('/')); UriBuilder httpHandlerUrlBuilder = new UriBuilder(strCurrentHost + "/fileUpload.ashx"); httpHandlerUrlBuilder.query = string.Format("{5}file={0}&offset={1}&last={2}&first={3}&DOCTYPE={4}&destination={6}",filename,this.dataSent,isLastChunk,isFirstChunk,DOCTYPE,string.IsNullOrEmpty(httpHandlerUrlBuilder.query) ? string.Empty : httpHandlerUrlBuilder.query.Remove(0,1) + "&",destinationOnServer); httpWebRequest webRequest = (httpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri); webRequest.Method = "POST"; webRequest.BeginGetRequestStream(new AsyncCallback(this.WritetoStreamCallback),webRequest); }
}
总结以上是内存溢出为你收集整理的.net – 使用HttpResponse在Silverlight中上传数据全部内容,希望文章能够帮你解决.net – 使用HttpResponse在Silverlight中上传数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)