Currently, I have implemented the file uploading in my tiny web server with C++.
Here are the logic of my apps:
- Parsing the
HTTP
message, get the upload file name and content. - Remove the old files with same file extension (business logic)
- Save the new file with uploading content into my apps folder
Code sample:
void remove_file(char* pfileextension) {
CFileFind finder;
CString SearchPath = MyApp::GetDirectory(DIR_ROOT) + _T("*.*");
BOOL bFind = finder.FindFile(SearchPath); //Search current work directory
while(bFind) {
bFind = finder.FindNextFile();
if(!finder.IsDirectory() &&
!finder.IsDots() &&
(!strcasecmp(finder.GetFileName().Right(4), pfileextension))) {
DeleteFile(finder.GetFilePath());
}
}
finder.Close();
}
bool save_upload_file(char* body, int len, char* filename, char* path){
char* pextension = strrchr(filename, '.');
char* paction = strrchr(path, '/');
// Filter out the license file and control file
if (!strcmp(paction, "/licensefile")){
if(!pextension || (pextension[0] == '\0') || strcasecmp(pextension, ".lic")){
send_error(200);
return false;
} else {
remove_file(".lic");
}
}
try{
CString szFilename = MyApp::GetDirectory(DIR_ROOT);
szFilename += CString(std::string(filename).c_str());
std::ofstream ofs (szFilename, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
if (ofs.fail()){
int err = GetLastError();
return false;
}
Now I test it with the case: upload the file under the my apps
. It works well under IE
, but fails under Chrome
.
After debugging it under Chrome
, I find the ofs.fail()
return true and the error code is 5
, which means Access Denied
. Further investigation, the file is still in my app folder after remove_file()
invoked, I think it may cause the ofs.fail()
. By Contrast, this file is removed under IE
after remove_file()
. Why??
Searching some knowledge, I guess it may be caused by sandbox
of render process in Chrome. I am confused...
Aucun commentaire:
Enregistrer un commentaire