开发者

Uploadify to database

开发者 https://www.devze.com 2023-01-09 04:09 出处:网络
i have a asp.net FileUpload control and below is the code (which works fine)   if (fUpload.HasFile)

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"


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);


If you are working behind a CMS or are having still having issues make sure you allow the location path in your web.config file as below.

<location path="Upload.ashx">
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</location>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号