asp.net mvc - C# MVC - Creating and uploading a zip file to FTP -
i creating xml file using following 2 methods:
public string getxmlstringfromfeedmodel<t>(t feedmodel) { var builder = new stringbuilder(); using (stringwriter writer = new encodingstringwriter(builder, encoding.utf8)) { xmlserializer serializer = new xmlserializer(typeof(t)); serializer.serialize(writer, feedmodel); return writer.getstringbuilder().tostring(); } } public filecontentresult getxmlfilefromxmlstring(string xmlstring, string filename ) { return new filecontentresult(encoding.utf8.getbytes(xmlstring), "application/xml") { filedownloadname = filename }; }
i zip file method
public filecontentresult zipfile(filecontentresult file, string filename) { using (var compressedfilestream = new memorystream()) { //create archive , store stream in memory. using (var ziparchive = new ziparchive(compressedfilestream, ziparchivemode.update, false)) { //create zip entry var zipentry = ziparchive.createentry(filename); //get stream of file using (var originalfilestream = new memorystream(file.filecontents)) { using (var zipentrystream = zipentry.open()) { //copy attachment stream zip entry stream originalfilestream.copyto(zipentrystream); } } } return new filecontentresult(compressedfilestream.toarray(), "application/zip") { filedownloadname = filename + ".zip" }; } }
then trying upload zipped file ftp server -
var model = getmodel(); var xmlstring = getxmlstringfromfeedmodel(model); var xmlfile = getxmlfilefromxmlstring(xmlstring); var zipped = zipfile(xmlfile); var credentials = getfeedcredentials(feedprovider); var memstream = new memorystream(zipped.filecontents) string filename = getfilenameforprovider(feedprovider); ftpwebrequest request = (ftpwebrequest)webrequest.create(credentials.url + "/" + filename); request.method = webrequestmethods.ftp.uploadfile; request.credentials = new networkcredential(credentials.username, credentials.password); var requeststream = request.getrequeststream(); memstream.copyto(requeststream); memstream.close(); requeststream.close(); ftpwebresponse response = (ftpwebresponse)request.getresponse(); return response;
the file uploaded , not empty (as looks around expected size) when copy ftp server computer , try open it, corrupt (error message, "file not readable, try password")
i can't see i'm doing wrong , have been struggling solve couple of days.
Comments
Post a Comment