简体   繁体   中英

IIS/ASP.NET - Request.Files Empty In Production

I am uploading a file to an ASP.NET ASPX page using HTTP POST from an Android phone. The process works perfectly when the web server used is the development server (Visual Studio 2013, IIS Express). The Request.Files collection contains my file, and all is right with the world.

The same code, running in production under IIS 7.5, still returns a response code of 200 (not 405!), but the Request.Files collection is completely empty.

For the life of me, I cannot figure out why. If the verb was being denied, I should have received a 405 code.

This the the Java code being executed on the phone:

URL postURL = new URL("redacted");

// Create the HTTP connection set our properties.
conn = (HttpURLConnection) postURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("uploaded_file", file.getName());

// Set up our stream writer.
dos = new DataOutputStream(conn.getOutputStream());

// Write the opening.
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="
    + file.getName() + ";filename=" + file.getName()
    + ";Content-Type: text/plain" + lineEnd);

dos.writeBytes(lineEnd);

// Grab our file and the stream object.
fileInputStream = new FileInputStream(file);

bytesAvailable = fileInputStream.available();

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

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

// Write the file contents.
while (bytesRead > 0) {
    dos.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

// Finish up writing our file.
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Close our stream objects.
fileInputStream.close();
dos.flush();
dos.close();

Is there some setting in IIS that I'm missing that's preventing that data from being propagated? Every example I've looked at for using HTTP POST to upload a file from Android without the Apache libs looks just like this.

I figured it out. IIS 7.5 is not as lenient as IIS Express when it comes to request properties, it seems.

I made the following changes:

// conn.setRequestProperty("Content-Type", "text/plain"); 
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);  

//dos.writeBytes("Content-Disposition: form-data; name="     + file.getName() + ";filename=" + file.getName()     + ";Content-Type: text/plain" + lineEnd); 
dos.writeBytes("Content-Disposition: form-data; name=\"" + file.getName() + \";filename=\"" + file.getName() + "\"" + lineEnd);

I tested each change by itself, and it wouldn't work. In order for the page to pick it up under IIS 7.5, I had to put quotes around the name of the file when writing it to the stream, and my content type had to be changed.

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