-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWFileUpload.C
More file actions
161 lines (127 loc) · 3.49 KB
/
WFileUpload.C
File metadata and controls
161 lines (127 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <boost/lexical_cast.hpp>
#include "Wt/WText"
#include "Wt/WFileUpload"
#include "Wt/WApplication"
#include "Wt/WContainerWidget"
#include "Wt/WEnvironment"
#include "DomElement.h"
#include "CgiParser.h"
namespace Wt {
WFileUpload::WFileUpload(WContainerWidget *parent)
: WWebWidget(parent),
uploaded(this),
fileTooLarge(this),
changed(this),
textSize_(20),
textSizeChanged_(false),
isStolen_(false),
doUpload_(false)
{
// NOTE: this is broken on konqueror: you cannot target a form anymore
methodIframe_ = WApplication::instance()->environment().ajax();
setInline(true);
if (!methodIframe_)
setFormObject(true);
}
WFileUpload::~WFileUpload()
{
if (!isStolen_)
unlink(spoolFileName_.c_str());
}
void WFileUpload::setFileTextSize(int chars)
{
textSize_ = chars;
textSizeChanged_ = true;
repaint(RepaintPropertyAttribute);
}
void WFileUpload::stealSpooledFile()
{
isStolen_ = true;
}
void WFileUpload::updateDom(DomElement& element, bool all)
{
if (methodIframe_ && doUpload_) {
element.callMethod("submit()");
doUpload_ = false;
}
WWebWidget::updateDom(element, all);
}
DomElementType WFileUpload::domElementType() const
{
return methodIframe_ ? DomElement_FORM : DomElement_INPUT;
}
DomElement *WFileUpload::createDomElement(WApplication *app)
{
DomElement *result = DomElement::createNew(domElementType());
result->setId(this, true);
if (methodIframe_) {
DomElement *form = result;
form->setAttribute("method", "post");
form->setAttribute("action", generateUrl());
form->setAttribute("enctype", "multipart/form-data");
form->setAttribute("style", "margin:0;padding:0;display:inline");
form->setProperty(PropertyTarget, "if" + formName());
DomElement *i = DomElement::createNew(DomElement_IFRAME);
i->setAttribute("class", "Wt-resource");
i->setAttribute("src", generateUrl());
i->setId("if" + formName(), true);
/*
* wrap iframe in an extra span to work around bug in IE which does
* not set the name use DOM methods
*/
DomElement *d = DomElement::createNew(DomElement_SPAN);
d->addChild(i);
form->addChild(d);
DomElement *input = DomElement::createNew(DomElement_INPUT);
input->setAttribute("type", "file");
input->setAttribute("name", "data");
input->setAttribute("size", boost::lexical_cast<std::string>(textSize_));
input->setId("in" + formName());
updateSignalConnection(*input, changed, "change", true);
form->addChild(input);
} else {
result->setAttribute("type", "file");
result->setAttribute("size", boost::lexical_cast<std::string>(textSize_));
updateSignalConnection(*result, changed, "change", true);
}
updateDom(*result, true);
return result;
}
void WFileUpload::setFormData(CgiEntry *entry)
{
if (!entry->clientFilename().empty()) {
spoolFileName_ = entry->value();
clientFileName_ = WString(entry->clientFilename(), UTF8);
contentDescription_ = WString(entry->contentType(), UTF8);
entry->stealFile();
isStolen_ = false;
}
resourceTriggerUpdate_ = true;
}
void WFileUpload::formDataSet()
{
uploaded.emit();
}
void WFileUpload::requestTooLarge(int size)
{
fileTooLarge.emit(size);
}
void WFileUpload::upload()
{
if (methodIframe_) {
doUpload_ = true;
repaint(RepaintPropertyIEMobile);
}
}
void WFileUpload::setNoFormData()
{
}
void WFileUpload::htmlText(std::ostream&)
{
}
}