Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit ce2a137

Browse files
Allow uploading files with invalid characters (Resolve #454) (#455)
1 parent 0a400bf commit ce2a137

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

AjaxControlToolkit.Tests/AjaxFileUploadTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class AjaxFileUploadTests {
1010
string _tempFolder;
1111
const string testBody = "------WebKitFormBoundaryCqenIHPHe1ZTCr0d\r\nContent-Disposition: form-data; name=\"act-file-data\"; filename=\"zero.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryCqenIHPHe1ZTCr0d--\r\n";
1212
const string testQuery = "filename=aaa.jpg&fileId=E63F2078-D5C7-66FA-5CAD-02C169149BD5";
13+
const string testQueryFilenameInvalidChars = "filename=a:a*a.jpg&fileId=E63F2078-D5C7-66FA-5CAD-02C169149BD5";
1314
const string testQueryNoExtension = "filename=aaa&fileId=E63F2078-D5C7-66FA-5CAD-02C169149BD5";
1415
const string testContentType = "multipart/form-data; boundary=----WebKitFormBoundaryCqenIHPHe1ZTCr0d";
1516

@@ -88,5 +89,13 @@ public void DoNotUseBufferlessInputStream() {
8889
var context = new HttpContext(request);
8990
Assert.DoesNotThrow(() => AjaxFileUploadHelper.Process(context));
9091
}
92+
93+
[Test]
94+
public void InvalidFilenameChars() {
95+
var request = new WorkerRequest(testBody, testQueryFilenameInvalidChars, testContentType);
96+
var context = new HttpContext(request);
97+
AjaxFileUploadHelper.Process(context);
98+
Assert.True(File.Exists(Path.Combine(_tempFolder, "E63F2078-D5C7-66FA-5CAD-02C169149BD5", "a-a-a.jpg.tmp")));
99+
}
91100
}
92101
}

AjaxControlToolkit/AjaxFileUpload/AjaxFileUploadHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ static void CreateTempFilePathFolder(string tmpFilePath) {
201201

202202
private static string GetTempFilePath(string fileId, string fileName) {
203203
var tempFolder = AjaxFileUpload.GetTempFolder(fileId);
204+
foreach (var invalidChar in Path.GetInvalidFileNameChars()) {
205+
fileName = fileName.Replace(invalidChar, '-');
206+
}
204207
return Path.Combine(tempFolder, fileName) + Constants.UploadTempFileExtension;
205208
}
206209
}

0 commit comments

Comments
 (0)