本文介绍了Httplistener和文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的网络服务器检索上传的文件.当客户端通过网络表单(随机文件)发送其文件时,我需要解析请求以获取文件并进一步处理它.基本上,代码如下:
I am trying to retrieve an uploaded file from my webserver. As the client sends its files through a webform (random files), I need to parse the request to get the file out and to process it further on. Basically, the code goes as:
HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; StreamReader r = new StreamReader(request.InputStream, System.Text.Encoding.Default); // this is the retrieved file from streamreader string file = null; while ((line = r.ReadLine()) != null){ // i read the stream till i retrieve the filename // get the file data out and break the loop } // A byststream is created by converting the string, Byte[] bytes = request.ContentEncoding.GetBytes(file); MemoryStream mstream = new MemoryStream(bytes); // do the rest因此,我能够检索文本文件,但对于所有其他文件,它们已损坏.有人能告诉我如何正确解析这些 HttplistnerRequests(或提供轻量级的替代方案)?
As a result, i am able to retrieve textfiles, but for all other files, they are corrupted. Could someone tell me how to parse these HttplistnerRequests properly (or providing a lightweighted alternative)?
推荐答案我认为通过使用 HttpListener 而不是使用 ASP 的内置工具,您正在使事情变得比必要的更难.网.但是如果你必须这样做,这里有一些示例代码.注意:1) 我假设您在
更多推荐
Httplistener和文件上传
发布评论