简体   繁体   中英

How to download or upload files using javascript(Client side scripting)

I'm using the below code in javascript for downloading or uploading a file from network.

$(document).ready(function DirectoryCopy(sourceDirName, destDirName, copySubDirs, test) {
        debugger;
        try {
            var dir = new System.IO.DirectoryInfo.ctor(sourceDirName);
            var directory_stop = dir.get_Name();
            var dirs = dir.GetDirectories();
            if (!dir.get_Exists()) {
                throw $CreateException(new System.IO.DirectoryNotFoundException.ctor$$String("Source directory does not exist or could not be found: " + sourceDirName), new Error());
            }
            if (!System.IO.Directory.Exists(destDirName)) {
                System.IO.Directory.CreateDirectory$$String(destDirName);
            }
            if (test == true) {
                System.IO.Directory.CreateDirectory$$String(destDirName + "\\" + "Complete");
            }
            var files = dir.GetFiles();
            for (var $i2 = 0, $l2 = files.length, file = files[$i2]; $i2 < $l2; $i2++, file = files[$i2]) {
                var temppath = System.IO.Path.Combine$$String$$String(destDirName, file.get_Name());
                file.CopyTo$$String$$Boolean(temppath, true);
            }
            if (copySubDirs) {
                for (var $i3 = 0, $l3 = dirs.length, subdir = dirs[$i3]; $i3 < $l3; $i3++, subdir = dirs[$i3]) {
                    var temppath = System.IO.Path.Combine$$String$$String(destDirName, subdir.get_Name());
                    DirectoryCopy(subdir.get_FullName(), temppath, copySubDirs, false);
                }
                return dirs.length;
            }
            return files.length;
        }
        catch (ex) {
            var path = "d:\\tempnew\\MyTest.txt";

            var sw = System.IO.File.CreateText(path);
            try {
                sw.WriteLine$$String(ex.toString());
            }
            finally {
                sw.Dispose();
            }

            return 0;
        }
    });

But I am getting an error on

 var dir = new System.IO.DirectoryInfo.ctor(sourceDirName);

as "System is not defined"

I'm passing values from code behind as shown below.

 TextBox1.Text = @"\" + "\\10.66.3.82" + @"\" + "ipadqc" + @"\" + "IPAD Titles" + @"\" + JobName.Text + @"\" + Issue.Text;


    string Macid = (string)(Session["Name"]);

    string path = "D:" + @"\" + "Ipad Download" + @"\" + Macid + @"\" + Process.Text + @"\" + JobName.Text + @"\" + Issue.Text;
    string a;

    ClientScript.RegisterStartupScript(typeof(Page), "script", a = "DirectoryCopy('" + TextBox1.Text + "','"+path+"', true, true);", true);

please correct me if i'm wrong or please let me know if there is any better option to do it.

System.IO is a .Net thing, not a javascript thing.

For downloading files using JS, there are plenty or questions like this on Stack Overlow already. like this one

As far as uploading goes, the top answer on this question pretty much sums it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM