Click here to Skip to main content
15,891,841 members
Articles / Hosted Services / Azure

Azure Animal Adoption Agent and Lost & Found

Rate me:
Please Sign up or sign in to vote.
5.00/5 (32 votes)
7 Oct 2020CPOL68 min read 115.2K   687   22  
Azure based pet adoption agent that helps pet lovers find the perfect pet while saving the lives of kittens & puppies
In this article, you will learn how to do convenient two-way data binding to an enumerated property in a Windows Phone app!
@{
    WebSecurity.RequireAuthenticatedUser();

    var galleryId = UrlData[0].AsInt();

    var db = Database.Open("PhotoGallery");

    var gallery = db.QuerySingle("SELECT * FROM Galleries WHERE Id = @0", galleryId);
    if (gallery == null)
    {
        Response.SetStatus(HttpStatusCode.NotFound);
        return;
    }

    Page.Title = "Upload Photo to Gallery - " + gallery.Name;
    if (IsPost)
    {
        var numFiles = Request.Files.Count;
        if(numFiles == 0)
        {
            ModelState.AddError("fileUpload", "Please specify at least one photo to upload.");
        } 
        else 
        {
            for (int i = 0; i < numFiles; i++)
            {
                var file = Request.Files[i];
                if (file.ContentLength > 0)
                {
                    var fileUpload = new WebImage(file.InputStream);
                    var fileTitle = Path.GetFileNameWithoutExtension(file.FileName).Trim();
                    if (fileTitle.IsEmpty())
                    {
                        fileTitle = "Untitled";
                    }
                    var fileExtension = Path.GetExtension(file.FileName).Trim();
                    var fileBytes = fileUpload.GetBytes();
                    db.Execute(@"INSERT INTO Photos
                        (GalleryId, UserId, Description, FileTitle, FileExtension, ContentType, FileSize, UploadDate, FileContents) VALUES 
                        (@0, @1, @2, @3, @4, @5, @6, @7, @8)", galleryId, WebSecurity.CurrentUserId, "", fileTitle, fileExtension,
                    fileUpload.ImageFormat, fileBytes.Length, DateTime.Now, fileBytes);
                }
            }
            Response.Redirect(Href("~/Photo/View", db.GetLastInsertId()));
        }
    }
}
<h1>Upload Lost Cat Photo</h1>

<p>The photo needs to be a picture of your cat looking at the camera, preferably straight 
    <br/>ahead and not looking up, down, or to the side.  The photo of your lost cat that you 
    <br/>upload will be placed in the
    <a class="italic" href="~/View/@galleryId" title="@gallery.Name">@gallery.Name</a> gallery.
    <br/><br/><b>Please only upload pictures of your own cat or pictures that are in the public domain . 
    <br/>Do <i>not</i> upload photos of cats found on the web that you do not have copyright 
    <br/>permissions for!</i></b></p>

<form method="post" enctype="multipart/form-data">
    @Html.ValidationSummary("Unable to upload:")
    <fieldset class="no-legend">
        <legend>Upload Photo</legend>
        @FileUpload.GetHtml(addText: "Add more files", uploadText: "Upload", includeFormTag: false)
        <p class="form-actions">
            <input type="submit" value="Upload" title="Upload photo" />
            <a href="~/View/@galleryId" title="Return to the &quot;@gallery.Name&quot; gallery.">Cancel</a>
        </p>
    </fieldset>
</form>

<p class="message info">
    The default file size is limited to 4MB. To change this you will need to update the 'HttpRuntimeSection/MaxRequestLength' section in web.config. 
</p>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Android Technologies, Inc.
United States United States
Robert Oschler is a veteran artificial intelligence, robotics, natural language processing, and speech recognition programmer. His latest love is C#/.NET programming, especially on the Windows Phone platform. When not writing code you can find him playing guitar or watching the latest videos on MSDN's Channel 9. He is also a member of the incredible Nokia DVLUP program and owes much of his affection for Windows Phone programming to the wonderfully talented and enthusiastic Nokia Ambassadors.

Comments and Discussions