views:

216

answers:

1

i have a asp.net FileUpload control and below is the code (which works fine)

 

 if (fUpload.HasFile)   
 {        
    string contentType = fUpload.PostedFile.ContentType;        
    string fileName = fUpload.PostedFile.FileName;       
    byte[] byteArray = fUpload.FileBytes;  
    ........         
 }

but i'm thinking of using the JQuery plugin Uploadify
how would you convert the above code in Uploadify?, i got stuck here

byte[] byteArray = fUpload.FileBytes;   // i dont find "FileBytes"
A: 

So if you are using the uploadify control you should have something in your markup like this:

<script type="text/javascript">
       // <![CDATA[
       var id = "55";
       var theString = "asdf";
       $(document).ready(function() {
       $('#fileInput').uploadify({
       'uploader': 'uploadify/uploadify.swf',
       'script': 'Upload.ashx',
       'scriptData': { 'id': id, 'foo': theString},
       'cancelImg': 'uploadify/cancel.png',
       'auto': true,
       'multi': true,
       'fileDesc': 'Image Files',
       'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
       'queueSizeLimit': 90,
       'sizeLimit': 4000000,
       'buttonText': 'Choose Images',
       'folder': '/uploads',
       'onAllComplete': function(event, queueID, fileObj, response, data) {

       }
     });
   });
   // ]]></script>

   <input id="fileInput" name="fileInput" type="file" />

Then you want to have a Generic Handler, which is an ashx file. What happens is this handler gets called when the uploadify control wants to upload one of the files in the Queue that it has. Open VS -> Right click your project -> Add New -> Choose Generic Handler -> Name it something like Upload.ashx.

Take that file and put something similar to this in there:

public class Upload : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpPostedFile file= context.Request.Files["Filedata"];

            int id = (Int32.Parse(context.Request["id"]));
            string foo = context.Request["foo"];
            file.SaveAs("C:\\" + id.ToString() + foo + file.FileName);

            context.Response.Write("1");
        }
        catch(Exception ex)
        {
            context.Response.Write("0");
        }
    }
}

It comes by default with an IsReusable()...DONT DELETE IT. Its required, just leave it in there or you will get a strange error.

Also, you can watch it step by step here: http://casonclagg.com/articles/6/video-tutorial-uploadify-asp-net-c-sharp.aspx

EDIT

I think you want to do this, where file is a HttpPostedFile object:

BinaryReader b = new BinaryReader(file.InputStream);
byte[] binaryData = b.ReadBytes(file.InputStream.Length);
Blankasaurus
thanks let me try and get back to you.
Abu Hamzah
Did you get it working?
Blankasaurus
i just started implementing this and i endup having issues, have you tried working with master page? with update-panel ?
Abu Hamzah
I'm not sure what you mean by working with Masterpage and UpdatePanel. Yes I have worked with both, but I'm not sure how that fits in with your question. What are your issues you are having?
Blankasaurus
where do i find the sample that uses masterpage/content page using updatepanel have you done similar to like this?
Abu Hamzah