简体   繁体   English

如何使用C#将附件对象保存到RackSpace Cloud?

[英]How To Save Attachments object to RackSpace Cloud using C#?

I Have one 'System.Net.Mail.Attachment[] attachment' object , This object contains PDF,Xls,Doc or jpg file. 我有一个“ System.Net.Mail.Attachment []附件”对象,该对象包含PDF,XLS,Doc或jpg文件。

I want to save this attachment object to cloud server. 我想将此附件对象保存到云服务器。

string sSavePath = "EmailAttachment/" + intSomeid + "/";
  string strErrorMsg = string.Empty;

 if ((attachments != null))
                            {
    MemoryStream memoryStream = new MemoryStream();
    StreamWriter memoryWriter = new StreamWriter(memoryStream);
    memoryWriter.Write(attachments[0]);
    memoryStream.Position = 0;
    CloudFileSystem.SaveFileToCloudSystem(memoryStream, ref strErrorMsg, sSavePath, ConfigHelper.PrivateContainer, attachments[intI].Name);
    memoryWriter.Dispose();
    memoryStream.Dispose();
}

I have used the above code to save the file. 我已经使用上面的代码保存了文件。 The File is saved to cloud but having 0 Byte data (Corrupted) File. 该文件已保存到云,但具有0字节数据(损坏)文件。 I have searched many places for that. 我已经搜索了很多地方。 But not able to find error in the code. 但是无法在代码中找到错误。

Please suggest some solution in this case ? 在这种情况下,请提出一些解决方案?

Looks like you are making it yourself more difficult then needed. 看起来您正在使自己变得更加困难,然后需要。 The Attachment instance has an ContentStream property which you don't need to feed through a MemoryStream at all. Attachment实例具有ContentStream属性,您根本不需要通过MemoryStream进行馈送。

string sSavePath = "EmailAttachment/" + intSomeid + "/";
string strErrorMsg = string.Empty;

if ((attachments != null))
{
   CloudFileSystem.SaveFileToCloudSystem(
     attachments[intI].ContentStream, 
     ref strErrorMsg, 
     sSavePath, 
     ConfigHelper.PrivateContainer, 
     attachments[intI].Name);
}

If you are doing this: 如果您这样做:

MemoryStream memoryStream = new MemoryStream();
StreamWriter memoryWriter = new StreamWriter(memoryStream);
memoryWriter.Write(attachments[0]);

You are probably writing the string representation of Attachment (ToString() gets called) and that is not the content of your file. 您可能正在编写Attachment的字符串表示形式(调用了ToString()),但这不是文件的内容。

After so much r&d ,I have come up with the following answer 经过如此多的研发,我想出了以下答案

Memory stream of attachment object didn't work for me. 附件对象的内存流对我不起作用。 So I have approached temp path where the attachement was saved and do the following magical code : 因此,我进入了保存附件的临时路径,并执行以下神奇代码:

string FileName = ((System.IO.FileStream (attachments[intI].ContentStream)).Name;
MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
}
ms.Position = 0;

CloudFileSystem.SaveFileToCloudSystem(ms, ref strErrorMsg, sSavePath, ConfigHelper.PrivateContainer, attachments[intI].Name);
ms.Dispose();

I hope my question and answer helps you for your project 希望我的问题和答案对您的项目有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM