Azhar
Azhar

Reputation: 20680

"500 error" in ASP.NET file-upload page?

I am calling this PHP code from Android to transfer file from Android to server and its working perfectly fine.

<?php
$target_path  = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
?>

But my server application is in .net and I have to write this code in .net I tried to write .net version code but it does not work and returns 500 internal server error.

public partial class uploadfiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HttpFileCollection uploadFile = Request.Files;
            if (uploadFile.Count > 0)
            {
                HttpPostedFile postedFile = uploadFile[0];
                System.IO.Stream inStream = postedFile.InputStream;
                byte[] fileData = new byte[postedFile.ContentLength];
                inStream.Read(fileData, 0, postedFile.ContentLength);
                postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName);
            }        
        }
        catch (Exception ex)
        { 
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Message : " +ex.Message);
            sb.AppendLine("Source : " + ex.Source);
            sb.AppendLine("StackTrace : " + ex.StackTrace);
            sb.AppendLine("InnerException : " + ex.InnerException);
            sb.AppendLine("ToString : " + ex.ToString());

            LogInToFile(sb.ToString());
        }
    }
}

It does not log any exception or even I think it does not reach to its first line. I checked it through Log file. as it does not work. please help.

Android code is below

HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;

String pathToOurFile = "/data/file_to_send.mp3";
String urlServer = "http://192.168.1.1/handle_upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();

fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}

Upvotes: 1

Views: 6146

Answers (2)

Ramesh
Ramesh

Reputation: 13266

The default max file size limit in ASP.NET application is 4 MB. You can configure the application to accept bigger size by setting the below in web.config

<system.web>
  <httpRuntime maxRequestLength="20480" />
</system.web>

To learn more about configuring the file size limit check out Large file uploads in ASP.NET

Upvotes: 4

BizApps
BizApps

Reputation: 6130

Try this:

postedFile.SaveAs(Server.MapPath("~/Data/" + postedFile.FileName ));

where ~/Data/ is the location where you want to save your file.

then + postedFile.FileName your filename.

Best Regards

Upvotes: 2

Related Questions