C# 文本文件上传和下载问题
好的,我有一个将文本文件上传到网络服务器的应用程序,一切正常。 但是,下载文本文件时,文本文件上会神奇地出现一行
示例:
textfile contains = Hello World
downloaded textfile contains = //notice the blank line here
Hello World
通常这不会成为问题,因为我只需创建一个临时文件并删除该行。
但是,由于文本文件包含加密数据,如果我创建一个新的临时文件来删除该行,它会完全混淆加密文本并创建 “错误数据”和“要解密的数据长度无效”错误。
我几乎 100% 确定这不是我的加密算法,因为文本文件是在上传之前输出的,并且它在未上传的文本文件上运行良好。
如果你们能帮助我那就太好了。任何解决办法都可以(无论它多么可怕/令人讨厌)。
Okay so I have an application which uploads a text file to a web server and all works fine.
However, a line magically appears on the text file when it is downloaded
example:
textfile contains = Hello World
downloaded textfile contains = //notice the blank line here
Hello World
Normally this wouldnt be a problem as I would just create a temp file and delete the line.
However, as the text file contains encrypted data and if I create a new temp file to delete the line it completely messes with the encrypted text and creates
"Bad Data" and "length of data to decrypt is invalid" errors.
Im almost 100% sure its not my encryption algorithm as the text files are output before they are uploaded and it works fine on the non uploaded text files.
If you guys could help me that would be awesome. Any work around will do (no matter how horrible / nasty it is).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
服务器和客户端是否运行相同系列的操作系统?我认为这可能是由于换行序列差异以及以不同模式(文本/二进制)上传和下载造成的。
如果数据经过加密或加密签名,您需要尽一切努力确保传输以二进制模式完成。
Does the server and client run the same family operating system? I'm thinking that this may be due to newline sequence differences, and uploading and downloading in different modes (text/binary).
If the data is encrypted or cryptographically signed, you want to do everything you can to make sure the transfers are done in binary mode.
下载代码是什么样的?
大胆猜测:您正在
Response.Write()
处理文本,而没有使用Response.Clear()
来清除任何“aspx 文本”。另外,您需要该代码以Response.End()
结尾,以防止进一步添加文本。What does the download code look like?
Making a wild guess: you are
Response.Write()
ing the text, without aResponse.Clear()
to clear any "aspx text". Plus you need that code to end on aResponse.End()
to prevent further additions to the text.看起来您的加密算法正在向您的文本附加以空结尾的字符串。
尝试以字节数组的形式加载网络服务器上的文本文件,并查看最后一个字节是否为“\0”。
It looks like your encryption algorithm is appending your text with null terminated string.
Try loading the text file on you webserver in a byte array and see if last byte is '\0'.
发生这样的事情有两个原因。
检查代码并发布一些示例(如果您是)实际上操纵它。我已经使用 C# 上传了文件,效果很好。
您应该查看 Hanselman 的博客,了解一个简单的上传应用程序...它很简单。
There are two reasons something like this can happen.
Check both the code and post some samples if you are actually manipulating it. I have uploaded files using c# and it works fine.
You should check Hanselman's blog for a simple upload application...It is straight forward.