C#:使用UploadFile方法将文件发送到PHP脚本,需要发送附加信息

时间:2017-04-14 20:44:13

标签: c# php mysql

我目前正在使用C#构建音乐应用程序。我需要将歌曲与特定用户相关联。我将文件发送到PHP脚本,然后将歌曲Title和Filepath上传到MariaDB数据库。我需要将用户的用户名从C#发送到PHP文件,这样我就可以将它作为外键与数据库中的歌曲Title和Filepath一起存储。我需要能够在将MP3文件从C#发送到PHP的同时将用户名传递给PHP。

感谢。

PHP

<?php

$fileExistsFlag = 0; 
$name = $_FILES['file']['name'];

//Variable that gets the username when passed from the client side(html)
$username = $_POST['username'];
//Variable that gets the username when passed from the client side(C#)
//$Uusername = $_POST['Uusername'];

$link = mysqli_connect(<sever details>) or die("Error ".mysqli_error($link));

//Checking whether the file already exists in the destination folder 
$query = "SELECT Title FROM songs WHERE Title='$name'"; 
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['Title'] == $name) {
$fileExistsFlag = 1;
}   
}

//If file is not present in the destination folder
if($fileExistsFlag == 0) { 
$target = "songs/"; 
$fileTarget = $target.$name;    
$tmp_name = $_FILES["file"]["tmp_name"];    
$result = move_uploaded_file($tmp_name,$fileTarget);


//If file was successfully uploaded in the destination folder
if($result) {   
echo "Your song <html><b><i>".$name."</i></b></html> has been successfully uploaded!";

$query = "INSERT INTO songs(Title,Path,Uusername) VALUES('$name','$fileTarget','$username')";
//$query = "INSERT INTO songs(Title,Path,Uusername) VALUES('$name','$fileTarget','$Uusername')";
//$query = "INSERT INTO songs(Title,Path) VALUES('$name','$fileTarget')";
$link->query($query) or die("Error : ".mysqli_error($link));
}

else {  
echo "Sorry. There was an error uploading your file.";
}
mysqli_close($link);
}


//If file is already present in the destination folder
else {
echo "The song <html><b><i>".$name."</i></b></html> already exists. Please rename the file and try again.";
mysqli_close($link);
}
?>

C#

private void uploadSongs()
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Multiselect = true;
    //string usernameEntry = LoginUsernameEntry.Text.ToLower();

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        try
        {

            files = openFileDialog1.SafeFileNames;
            paths = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length; i++)
            {
                filelist.Items.Add(files[i]);
                this.nowplaying.Text = openFileDialog1.FileName;
                MessageBox.Show(openFileDialog1.FileName);
                //sendUsername();

            filelist.Items.Add(files[i]);

            MessageBox.Show(openFileDialog1.FileName);


                //variable that the LoginUsername is set to so it can be passed to the PHP
                string usernameEntry = LoginUsernameEntry.Text.ToLower();                        

                //code that uploads files to the server by creating a new instance of the WebClient class
                System.Net.WebClient Client = new System.Net.WebClient();

                //setting the content type of the files being uploaded
                Client.Headers.Add("Content-Type", "binary/octet-stream");

                //reading in the file as bytes, using UploadFile to call the PHP "POST" the file, with the name being "openFileDialog.1.FileName"
                byte[] result = Client.UploadFile("URL", "POST", openFileDialog1.FileName);

                //send the resulting bytes as a string to the server
                string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);

                string response = System.Text.Encoding.ASCII.GetString(result);

                MessageBox.Show(response);

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }


    }

}

0 个答案:

没有答案