ParthKansara February 2016

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.

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. I have searched many places for that. But not able to find error in the code.

Please suggest some solution in this case ?

Answers


Mirouf February 2016

I think that's because you don't read your file. My code is Vb.net but you should see this link. This one explain how to send file via http in C#.

http://www.techcoil.com/blog/sending-a-file-and-some-form-data-via-http-post-in-c/

        Dim file As String = String.Empty
        Dim sr As New StreamReader(path)

        file = sr.ReadToEnd
        sr.Close()
        sr.Dispose()

        bytesToWrite = Encoding.UTF8.GetBytes(file)

        ' Create a http request to the server endpoint that will pick up the
        ' file and file description.
        Dim requestToServerEndpoint As HttpWebRequest = CType(WebRequest.Create(lienNaviguea), HttpWebRequest)

        ' Set the http request header \\
        requestToServerEndpoint.Method = WebRequestMethods.Http.Post
        requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString
        requestToServerEndpoint.Credentials = System.Net.CredentialCache.DefaultCredentials

        ' Use a MemoryStream to form <span class="IL_AD" id="IL_AD12">the post</span> data request,
        ' so that we can get the content-length <span class="IL_AD" id="IL_AD4">attribute</span>.
        Dim postDataStream As MemoryStream = New MemoryStream()
        Dim postDataWriter As StreamWriter = New StreamWriter(postDataStream)

        ' Include the file in the post data
        postDataWriter.Write(vbCrLf + "--" + boundaryString + vbCrLf)
        postDataWriter.Write("Content-Disposition: form-data;" _
                                + "name=""{0}"";" _
                                + "filename=""" + Path.GetFileName(path) + """" _
                                + vbCrLf + "Content-Type: " + Path.GetExtension(path) + vbCrLf + vbCrLf, "form[report]")

        postDataWriter.Flush()


        ' Read the file
        Dim f 


rene February 2016

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.

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.


ParthKansara February 2016

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

Post Status

Asked in February 2016
Viewed 1,237 times
Voted 5
Answered 3 times

Search




Leave an answer