Exemple #1
0
    public void RaisePostBackEvent(string eventArgument)
    {
        var strings = eventArgument.Split('|');

        if (strings.Length == 2)
        {
            var type     = strings[0];
            var filename = strings[1];
            if (type == "Download")
            {
                DirectoryInfo root      = new DirectoryInfo(UploadedFile.filePath);
                FileInfo[]    listfiles = root.GetFiles(filename);


                if (listfiles.Length != 0)
                {
                    Response.ClearContent();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + listfiles[0].Name);
                    Response.AddHeader("Content-Length", listfiles[0].Length.ToString());
                    Response.ContentType = "application//octet-stream";

                    Response.TransmitFile(listfiles[0].FullName);
                    Response.End();
                }
            }
            if (type == "Delete")
            {
                string[] file = filename.Split('.');
                UploadedFile.DeleteExistingFiles(file[0]);
                Response.Cookies["lastFile"].Expires   = DateTime.Now.AddDays(-1);
                Response.Cookies["lastTicket"].Expires = DateTime.Now.AddDays(-1);
            }
        }
    }
Exemple #2
0
    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        string lastFile = "";

        try
        {
            lastFile = Request.Cookies["lastFile"].Value;
        }
        catch { }

        string lastTicket = "";

        try
        {
            lastTicket = Request.Cookies["lastTicket"].Value;
        }
        catch { }

        string fileExt = UploadedFile.GetFileExtention(AsyncFileUpload1.FileName);

        // The user had uploaded a second file see if one with the id and any extension exists and delete it to make room for the new one
        UploadedFile.DeleteExistingFiles(lblIndex.Text);


        if (UploadedFile.FileExtentionAllowed(fileExt))
        {
            //Get real path to upload files to
            if (lastFile != AsyncFileUpload1.FileName || lastTicket != lblIndex.Text)
            {
                if (lblIndex.Text != "")
                {
                    try
                    {
                        DirectoryInfo root      = new DirectoryInfo(UploadedFile.filePath);
                        FileInfo[]    listfiles = root.GetFiles(lblIndex.Text + "*");
                        Array.Sort(listfiles, delegate(FileInfo listfile1, FileInfo listfile2)
                        {
                            return(listfile1.Name.CompareTo(listfile2.Name));
                        });
                        if (listfiles.Length != 0)
                        {
                            string currentFile       = listfiles[listfiles.Length - 1].FullName;
                            int    currentFileNumber = int.Parse(currentFile.Split('_')[1].Split('.')[0]);
                            currentFileNumber++;
                            string nextFileNumber = currentFileNumber.ToString();
                            if (currentFileNumber < 10)
                            {
                                nextFileNumber = "0" + nextFileNumber;
                            }
                            if (UploadedFile.FileExtentionAllowed(fileExt))
                            {
                                if (currentFileNumber <= UploadedFile.MaxUploads())
                                {
                                    AsyncFileUpload1.SaveAs(UploadedFile.filePath + lblIndex.Text + "_" + nextFileNumber + "." + fileExt);
                                }
                                else
                                {
                                    AsyncFileUpload1.SaveAs(UploadedFile.filePath + lblIndex.Text + "_" + UploadedFile.MaxUploads() + "." + fileExt);
                                }
                            }
                            else
                            {
                                lblFileIssue.Visible = true;
                            }
                        }
                        else if (UploadedFile.FileExtentionAllowed(fileExt))
                        {
                            AsyncFileUpload1.SaveAs(UploadedFile.filePath + lblIndex.Text + "_01." + fileExt);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
        ClearContents(AsyncFileUpload1 as Control);
        Response.Cookies["lastFile"].Value     = AsyncFileUpload1.FileName;
        Response.Cookies["lastFile"].Expires   = DateTime.Now.AddDays(1);
        Response.Cookies["lastTicket"].Value   = lblIndex.Text;
        Response.Cookies["lastTicket"].Expires = DateTime.Now.AddDays(1);
    }