FileUpload 使用 ftp 上传后文件被清除
使用 ftp 上传后,我在处理上传的文件时遇到问题。
用户上传的文件包含xml。我必须将此文件保存到磁盘,但我无法将此文件直接写入磁盘,因此我使用 ftp 来保存它。保存后,需要对其进行处理以将其内容保存在数据库中。 XmlReader 失败并显示错误“未检测到根元素”。经过一番调试后我得出结论该文件是空的。
我尝试复制文件(其中 xsdUpload 是用户上传的文件):
FileUpload test = new FileUpload();
test = xsdUpload;
但我不太确定这是否也会复制内存中的文件。
ftp上传功能如下:
public string uploadXsd(string fileName, FileUpload xsd)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + '/' + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
StreamReader sourceStream = new StreamReader(xsd.FileContent);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
return url + '/' + fileName;
}
该功能工作正常。我可以在它应该所在的目录中看到该文件。
处理我使用的文件:
XmlReader reader = XmlReader.Create(xsdUploaded.FileContent);
while (reader.Read())
{
if (reader.Name != "")
{
有人有解决方案吗?
I'm having trouble processing a uploaded file after it has been uploaded with ftp.
The file the user uploads contains xml. I have to save this file to disk but I'm not able to write this file directly to disk so I use ftp to save it. After it is saved it needs to be processed to save it contents in a database. The XmlReader fails with the error "No root element detected". After some debugging I came to the conclusion the file is empty.
I tried to copy the file (where xsdUpload is the file uploaded by the user):
FileUpload test = new FileUpload();
test = xsdUpload;
But I'm not really shure if this also copys the file in memory.
The ftp upload function is as followed:
public string uploadXsd(string fileName, FileUpload xsd)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + '/' + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
StreamReader sourceStream = new StreamReader(xsd.FileContent);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
return url + '/' + fileName;
}
This function works fine. I can see the file in the directory where it supposed to be.
To process the file I use:
XmlReader reader = XmlReader.Create(xsdUploaded.FileContent);
while (reader.Read())
{
if (reader.Name != "")
{
Does anyone have a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 FileContent 属性是一个 Stream 类实例,您应该在第二次阅读之前将其从头开始,即
我不确定,希望这会有所帮助。
此致。
It seems that FileContent property is a Stream class instance and you should Seek it to the beginning before the second reading, i.e.
I am not sure, hope this helps.
Best regards.