FileUpload 使用 ftp 上传后文件被清除

发布于 2024-12-29 10:30:24 字数 1416 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2025-01-05 10:30:24

看来 FileContent 属性是一个 Stream 类实例,您应该在第二次阅读之前将其从头开始,即

xsdUploaded.FileContent.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(xsdUploaded.FileContent);

我不确定,希望这会有所帮助。

此致。

It seems that FileContent property is a Stream class instance and you should Seek it to the beginning before the second reading, i.e.

xsdUploaded.FileContent.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(xsdUploaded.FileContent);

I am not sure, hope this helps.

Best regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文