Handling uploading files

Eugene Alfonso's Avatar

Eugene Alfonso

30 Jun, 2011 05:31 AM

My colleague and I are looking into hosting one of our clients web applications on AppHarbor and we have a question about handling file uploads. We are building a real-estate application similar to Zillow and one of the features is that home-sellers can upload images to their properties and profile. We are currently using a FileStream type from SQL Server for this, and want to know if AppHarbor supports this? If not, do you support uploading to the file system? Are there any folder/size constraints we should be aware of for applications? And lastly, what version of SQL Server are you using?

Thank you for your time.

  1. 2 Posted by balexandre on 30 Jun, 2011 05:43 AM

    balexandre's Avatar

    Hi Eugene,

    I have such application and I took another path. I have a SaaS application and my clients can upload their own graphics for their Calendar and Frontend Website directly in the Administration area, but I do not use IIS for that.

    I created an Amazon S3 account, added a bucket and started uploading the files directly to S3, in this way I could easily control the access, divide the bandwith of the hosting server to only take care about real tasks, and S3 would take care of handling the files.

    Plus, you can sign in to Cloud Front and make that S3 bucket a CDN.

    All you have to do is use the fantastic Amazon SDK for .NET in your application (assuming you are using ASP.NET MVC 3 or similiar) like:

    public void UploadApplicationFile(FileUploadInfo file, string type, Guid? calendarGuid, string filePath = "")
    {
        if (file != null && file.ImageToUpload != null && file.ImageToUpload.ContentLength > 0)
        {
            if (IsValidFile(file.ImageToUpload.ContentType))
            {
                // The user selected a file to upload => handle it
                FileInfo fi = new FileInfo(Path.GetFileName(file.ImageToUpload.FileName));
    
                string fileName = SaveFileName
                                    .Replace("{calendar}", calendarGuid.HasValue ? calendarGuid.Value.ToString() : "")
                                    .Replace("{type}", type)
                                    .Replace("{ext}", fi.Extension.Replace(".", ""));
    
                string path = Path.Combine(filePath, fileName);
                file.ImageUrl = fileName;
    
                // Upload to Amazon S3 
                this.UploadToAmazonService(file, fileName);
            }
        }
    }
    
    private void UploadToAmazonService(FileUploadInfo file, string filename)
    {
        string accessKey = System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"];
        string secretAccessKey = System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"];
        string bucketName = System.Configuration.ConfigurationManager.AppSettings["AWSPublicFilesBucket"];
    
        string publicFile = "PublicFiles/" + filename;
    
        PutObjectRequest request = new PutObjectRequest();
        request.WithBucketName(bucketName);
        request.WithKey(publicFile);
        request.WithInputStream(file.ImageToUpload.InputStream);
        request.AutoCloseStream = true;
        request.CannedACL = S3CannedACL.PublicRead;
    
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey);
        using (S3Response r = client.PutObject(request)) { }
    }
    
    private bool IsValidFile(string contentType)
    {
        return
            contentType.Contains("png")
            || contentType.Contains("bitmap")
            || contentType.Contains("jpeg")
            || contentType.Contains("gif")
            || contentType.Contains("flash"); // application/x-shockwave-flash
    }
    

    This way you can divide things and save some bandwith in the hosting server to do what is meant for, processing requests...

    I hope it helps giving you another way of accomplish the same..

  2. Support Staff 3 Posted by friism on 30 Jun, 2011 05:58 AM

    friism's Avatar

    Eugene, Bruno's suggestions is a good one, and what we recommend. You should be able to use SQL Server too though. We currently run SQL Server 2008 R2 Express.

  3. friism closed this discussion on 30 Jun, 2011 05:58 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac