Solved

Transition from PHP to ASP.net

Posted on 2010-11-11
4
737 Views
Last Modified: 2012-05-10
Hi.

I'm starting to code in ASP.NET, and i have the following question.

I want to code a program to upload a file using "actionscript 2.0" ( flash.net.FileReference) on VB.NET; right now, i know how to do this using Perl and PHP ( "move_uploaded_file($_FILES['Filedata']['tmp_name'], "./imgage_files/".$_FILES['Filedata']['name'])"),
but i can't find how to accomplish the same task using this language.

I hope you could help me, and thank you very much.
0
Comment
Question by:kalvillo
  • 2
4 Comments
 

Expert Comment

by:tzuhsun
ID: 34117415
Hi i can't give you a quick answer as I no idea right now.

But you can try search in goole by uing such phrase:
"ASP.net equivalient to move_uploaded_file()"

most of the time would not disappoint you.
0
 
LVL 7

Accepted Solution

by:
niralshah earned 500 total points
ID: 34119333
See the attached code.
flash script file
package
{
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    import flash.utils.ByteArray;
    import fl.controls.Button;
    import com.adobe.images.JPGEncoder;
    import com.dynamicflash.util.Base64;


    public class Uploader extends MovieClip
    {
        // Reference to the button on the stage
        public var btnUpload:Button;

        // Encoder quality
        private var _jpegQuality:int = 100;

        // Path to the upload script
        private var _uploadPath:String = "/upload.aspx";

        public function Uploader()
        {
                btnUpload.addEventListener(MouseEvent.CLICK, buttonClick);
        }

        private function buttonClick(e:MouseEvent):void
        {
                       // Create a new BitmapData object the size of the upload button.
                       // We're going to send the image of the button to the server.
                var image:BitmapData = new BitmapData(btnUpload.width, btnUpload.height);
                       // Draw the button into the BitmapData
                image.draw(btnUpload);

                       // Encode the BitmapData into a ByteArray
                var enc:JPGEncoder = new JPGEncoder(_jpegQuality);
                var bytes:ByteArray = enc.encode(image);

                       // and convert the ByteArray to a Base64 encoded string
                var base64Bytes:String = Base64.encodeByteArray(bytes);

                       // Add the string to a URLVariables object
                var vars:URLVariables = new URLVariables();
                vars.imageData = base64Bytes;

                // and send it over the wire via HTTP POST
                var url:URLRequest = new URLRequest(_uploadPath);
                url.data = vars;
                url.method = URLRequestMethod.POST;

                var loader:URLLoader = new URLLoader();
                loader.load(url);
        }
    }
}


asp.net code upload.aspx page

using System;
using System.IO;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get the data from the POST array
        string data = Request.Form["imageData"];

        // Decode the bytes from the Base64 string
        byte[] bytes = Convert.FromBase64String(data);

        // Write the jpeg to disk
        string path = Server.MapPath("~/save.jpg");
        File.WriteAllBytes(path, bytes);

        // Clear the response and send a Flash variable back to the URL Loader
        Response.Clear();
        Response.ContentType = "text/plain";
        Response.Write("ok=ok");
    }
}

Open in new window

0
 

Author Comment

by:kalvillo
ID: 34123470
niralshah> that's not exactly what i'm looking for...

Right now, i have the following Flash code to "browse" my files:

import flash.net.FileReference;

_root.http_file = "http://myDom.com/upload.php";

// start exploration
btnListener.click = function(eventObj:Object) {
	var fileRef:FileReference = new FileReference();
	fileRef.addListener(fileListener);
	fileRef.browse(fileTypes);
}

//send file selected
fileListener.onSelect = function(file:FileReference):Void {
   resultsTxtField.text += "File Selected: " + file.name;
   if(!file.upload(_root.http_file)) {
       resultsTxtField.text += "Upload dialog failed to open.\n";
   }
}

Open in new window


When you select the desired file in the dialog box, Flash sends the file to the following code in PHP:

<?php
	move_uploaded_file($_FILES['Filedata']['tmp_name'], "./file_images/".$_FILES['Filedata']['name']);
	chmod("./file_images/".$_FILES['Filedata']['name'], 0777);
?>

Open in new window


This PHP code delivers the file into the server.

What i need is a code in ASP.NET that do the same thing than the PHP one... i mean, the Flash code works fine as it is, i just need a code in ASP to conect with such flash code.
0
 

Author Comment

by:kalvillo
ID: 34124993
niralshah> by the way.. that code that you send me, do you have it on "Action Script 2"?... it could be handy for another process.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question