简体   繁体   中英

Need to move and handle file from one server with C# to anoter server with php?

I have two websites online ie 1st is PM which is developed using asp.net, C# ,sql server and 2nd one is developed using php ,mysql.

I need to manage data by choosing one website for CRUD operations (asp.net) and on another website(php) the viewer can only view data (nserted by asp.net website).

as the information viewed is same but on different websites.

I need to insert persons data with 3 sample files on asp .net website and insert and move info + samples fies to the php website .

I really dont have any idea to achieve that ,I can use Webclient() to post data to php web but still how can i move files to the php page .

Can i pass file path from c# to PHP using webclient /ajax post to upload file from one server to another ?

comment posted said !

"a file input is for sending files from your computer to the server. PHP cannot "reach out" to your local computer and grab a file, even if you pass it the full path. This is NOT possible as it would be a massively HUGE security hole."

Passing file path to php script

Need help :(

kindly repli

You can install and set up ftp software on the php server, and then you can use FtpWebRequest in c# to upload the file:

    private FtpWebRequest GetRequest(string requestUri, string method, bool keepAlive)
    {
        FtpWebRequest retVal = null;

        try
        {
            retVal = FtpWebRequest.Create(requestUri) as FtpWebRequest;
            retVal.Method = method;
            retVal.Credentials = new NetworkCredential(username, password);
            retVal.UsePassive = true;
            retVal.UseBinary = true;
            retVal.KeepAlive = keepAlive;
           // retVal = request.GetRequestStream();


        }
        catch (Exception ex)
        {

        }

        return retVal;
    }

    public bool UploadFile(string fileToUpload)
    {
        bool retVal = false;


        FtpWebRequest request = null;
        Stream outstream = null;
        FileStream fs = null;

        try
        {
            request = GetRequest(String.Format("{0}/{1}", host, Path.GetFileName(fileToUpload)), WebRequestMethods.Ftp.UploadFile, false);


            byte[] buffer = new byte[4096];
            int count = 0;

            outstream = request.GetRequestStream();

            using (FileStream streamReader = File.OpenRead(fileToUpload))
            {
                int read;
                while ((read = streamReader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Console.WriteLine("Writing to FTP stream: " + (count += read));
                    outstream.Write(buffer, 0, read);
                    outstream.Flush();
                }
            }

            retVal = true;
        }
        catch (Exception ex)
        {

        }
        finally
        {
            Console.WriteLine("Closing the FTP stream");

            if (request != null)
            {
                request.Abort();
            }

            if (outstream != null)
                outstream.Close();
        }

        return retVal;


    }

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