Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I recently acquired file space on the Rackspace Cloud. I'm using the Cloud File product. I can manually create my containers and upload files to them manually via the control panel, and I can get my connection through their API and list all the files in the container. What I'm trying to do now is to create an ASP.NET C# page that allows the user to upload their file from their computer to my Cloud space.

I went through the documentation, but it's not clear to me. My code below returns an error saying the item does not exist. I don't know if that means the source or destination.

C#
protected void btnUpload_Click(object sender, EventArgs e)
   {
       string localFolder = @"C:\songs\";
       string songName = "somesong.mp3";
       net.openstack.Core.Domain.CloudIdentity cloudIdentity = new net.openstack.Core.Domain.CloudIdentity() { APIKey = "myAPIkey", Username = "myUserName" };
       var cloudFilesProvider = new net.openstack.Providers.Rackspace.CloudFilesProvider(cloudIdentity);
       cloudFilesProvider.GetObjectSaveToFile("songs", localFolder, songName, songName, region: "DFW");
   }


I also tried the CreateObjectFromFile method, and I get an error that says it can't find "c:\songs\", but it is there.

cloudFilesProvider.CreateObjectFromFile("songs", localFolder, songName);


Thanks for any help, this is driving me nuts!!!
Posted
Comments
David_Wimbley 26-May-13 23:02pm    
Do you HAVE to use the cloud file upload product thing? Or is the a must?
Motley Drew 26-May-13 23:09pm    
I'm honestly not sure. I found their sample code (and fixed it because their stuff didn't work) and it works fine for reading the files, I just can't figure out how to upload to the Cloud. Are there other options?
David_Wimbley 26-May-13 23:39pm    
Ill post some code as solution in a minute...forgive me im no good at webforms (i started asp.net with mvc 3).

So in webforms to upload a file here is my default.aspx.cs Submit1 click event

C#
private void Submit1_ServerClick(object sender, System.EventArgs e)
        {
            if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
            {
                string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                //This Data is the folder inside my project to save uploads...rename accordingly
                string SaveLocation = Server.MapPath("Data") + "\\" + fn;
                try
                {
                    File1.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded.");
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);
                }
            }
            else
            {
                Response.Write("Please select a file to upload.");
            }
        }


Below is my Default.aspx HTML for the upload

XML
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
        <input type=file id=File1 name=File1 runat="server" />

        <input type="submit" id="Submit1" value="Upload" runat="server" NAME="Submit1"/>
    </form>
</asp:Content>



If you are able to do it via MVC you could do the following

//Disclaimer -- I should have used the Html helper for this but i was being lazy. /Ajax/AddAttachment is the Controller/method name.
XML
<form method="post" enctype="multipart/form-data" class="asform-inline" action="/Ajax/AddAttachment">
                                <input type="hidden" name="IssueID" id="IssueID" value="@Model.IndexID" />
                                <input type="file" name="files">

                                <input type="submit" name="submit" value="Add Attachment" class="btn btn-primary" />
                            </form>



C#
[HttpPost]
public ActionResult AddAttachment(string IssueID, IEnumerable<HttpPostedFileBase> files)
{
    using (UnitOfWork uow = new UnitOfWork())
    {
        if (files != null)
        {
            foreach (var file in files)
            {
                if (file != null)
                {
                    if (file.ContentLength > 0)
                    {
                        string fileName = Path.GetFileName(file.FileName);
                        string path = Path.Combine(Server.MapPath("~/Content/attachments"), fileName);
                        file.SaveAs(path);

                        if (System.IO.File.Exists(path))
                        {
                            string filename = string.Format(@"{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(fileName));
                            System.IO.File.Move(path, Path.Combine(Server.MapPath("~/Content/attachments"), filename));
                            path = Path.Combine(Server.MapPath("~/Content/attachments"), filename);
                        }
                    }
                }
            }
        }
        return RedirectToAction("View", "Controller", new { @id = IssueID });
    }
}


I did test the web forms one before posting so i do know it uploads files. Im pretty sure its not efficient at all (if it is thats sad) so it could be improved upon. The MVC file upload is merely meant to show a comparison on how file uploads are handled in mvc 3 and greater.

If you have any questions on what i've posted i'll be happy to try and help you out.
 
Share this answer
 
Comments
Motley Drew 27-May-13 0:08am    
Thanks for the info. I have done file uploads to my web server before. This scenario is different. My web host and my Cloud server are not on the same server, so the Server.MapPath will not work in my case.
David_Wimbley 27-May-13 0:39am    
Ah ok so thats what i did not realize...my bad. Maybe rackspace has a support forum that might be better suited for this type of specific question?

A round about way of doing it would be to have a web service on whatever your file server is and have it expose a method to that accepts a file stream and then save the file to server that way...but that may be over doing it.

Hope you get better help then what i provided!
I solved this. I was running locally, and that was the problem. When I uploaded to my web server, it worked. It still seems like a pain in the butt to have to first upload to the web server, then copy to the cloud, then delete from the web server. The cloud is meant to handle large bandwidth requests, but web servers are not intended for ongoing large data transfers to the same capacity.

Thank you all for your help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900