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?
It seems that FileContent property is a Stream class instance and you should Seek it to the beginning before the second reading, ie
xsdUploaded.FileContent.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(xsdUploaded.FileContent);
I am not sure, hope this helps.
Best regards.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.